I am looking at Apple's MapCallouts example for map annotations and callouts (==bubbles that appear when you click on a pin). Every annotation there has coordinates, title and subtitle. I would like to display subtitle in 2 lines, i tried with:
- (NSString *)subtitle
{
return @"Founded: June 29, 1776\nSecond line text";
}
but the text "Second line text" stays in one line and makes bubble wider.I get this:
I would also like to change image of the button to one of my own's, the code that sets the button is currently like this:
UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
Any ideas?
EDIT: I tried 7KV7's advice. Button change is successful, but i still cant get subtitles in 2 lines. My code:
MKPinAnnotationView* customPinView = [[[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:BridgeAnnotationIdentifier] autorelease];
customPinView.pinColor = MKPinAnnotationColorPurple;
customPinView.animatesDrop = YES;
// Button
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 23, 23);
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
//UIEdgeInsets titleInsets = UIEdgeInsetsMake(7.0, -20.0, 7.0, 7.0);
//button.titleEdgeInsets = titleInsets;
[button setImage:[UIImage imageNamed:@"ic_phone_default.png"] forState:UIControlStateNormal];
//[button setImage:[UIImage imageNamed:@"ic_phone_selected.png"] forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:@"ic_phone_selected.png"] forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(showDetails:) forControlEvents:UIControlEventTouchUpInside];
customPinView.rightCalloutAccessoryView = button;
//two labels
UIView *leftCAV = [[UIView alloc] initWithFrame:CGRectMake(0,0,50,50)];
//[leftCAV addSubview : button];
UILabel *l1=[[UILabel alloc] init];
l1.frame=CGRectMake(0, 15, 50, 50);
l1.text=@"First line of subtitle";
l1.font=[UIFont fontWithName:@"Arial Rounded MT Bold" size:(10.0)];
UILabel *l2=[[UILabel alloc] init];
l2.frame=CGRectMake(0, 30, 50, 50);
l2.text=@"Second line of subtitle";
l2.font=[UIFont fontWithName:@"Arial Rounded MT Bold" size:(10.0)];
[leftCAV addSubview : l1];
[leftCAV addSubview : l2];
customPinView.leftCalloutAccessoryView = leftCAV;
//customPinView.
customPinView.canShowCallout = YES;
return customPinView;
I get this:
Result Image
This solution sets "justified" text, hope be useful:
Swift 3:
This is how I achieved it without subclassing the annotation view, or using undocumented APIs:
You can customize the font/color of the text by adjusting
title
andsubtitle
accordingly.Sure it's a bit of a hack, but it achieves the desired results without subclassing, and allows you to customize with whatever additional buttons/images you'd like to add
calloutView
(and you can still place buttons in the right-accessory view if you wish, as long as you retain the height).You won't be able to do it with public API and will have to reimplement your own custom annotation view.
You can have a look at this code that reimplements the annotation view but mimics the standard iOS one, and you can then enrich it to your own need :
http://blog.asolutions.com/2010/09/building-custom-map-annotation-callouts-part-1/
For setting the button you can use the below code
It can be done in 9 steps.
I created a label and added it to the
annotationView?.detailCalloutAccessoryView
property (step 5). I also set the label's text to theannotation.subtitle
text (step 2 & step 4). I set the label's.numberOfLines = 0
.This will make the subtitle extend however many # of lines needed (set to 2 if you only need 2).For the button I created a UIButton and set it's type to
.detailDisclosure
(step 7). I then set the button's image property to the custom image I want to use (step 8). And finally I set the button to the annotationView's.rightCalloutAccessoryView
property (step 9).I also offset the annotationView's
.calloutOffset
property (step 6). I got the offset from ray wenderlichSteps are in the comments above the code
UPDATE
The default style for annotations only supports the title and subtitle. Neither title nor subtitle can include line breaks. You cannot do this without subclassing.
To use a custom view review Apple's sample code:
http://developer.apple.com/library/ios/#samplecode/WeatherMap/Introduction/Intro.html
I also think there is a problem in your code
l1 has a frame (0, 15, 50, 50) and l2 has (0, 30, 50, 50). Wont these two overlap? I mean l1 will start from y=15 and its height is 50. so when l2 starts from 30 it may overlap.. Can you pls check by changing the frames