iphone - 如何显示 MKAnnotation 的副标题 2 行文本并更改右侧按钮的图像?

标签 iphone mkmapview mkannotation

我正在查看 Apple 的 MapCallouts 示例,了解 map 注释和标注(==单击图钉时出现的气泡)。 每个注释都有坐标、标题和副标题。 我想用两行显示字幕,我尝试过:

- (NSString *)subtitle
{
return @"Founded: June 29, 1776\nSecond line text";
}

但是文本“第二行文本”保留在一行中并使气泡变宽。我明白了:

enter image description here

我还想将按钮的图像更改为我自己的图像,设置按钮的代码目前如下所示:

UIButton* rightButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

有什么想法吗?

编辑:我尝试了 7KV7 的建议。按钮更改成功,但我仍然无法获得两行字幕。 我的代码:

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;

我明白了:

enter image description here

最佳答案

请使用这个

 - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation {

        if ([annotation isKindOfClass:[MKUserLocation class]])
            return nil;
        if ([annotation isKindOfClass:[CustomAnnotation class]]) {
            CustomAnnotation *customAnnotation = (CustomAnnotation *) annotation;

            MKAnnotationView *annotationView = [mapView dequeueReusableAnnotationViewWithIdentifier:@"CustomAnnotation"];

            if (annotationView == nil)
                annotationView = customAnnotation.annotationView;
            else
                annotationView.annotation = annotation;

            //Adding multiline subtitle code 

            UILabel *subTitlelbl = [[UILabel alloc]init];
            subTitlelbl.text = @"sri ganganagar this is my home twon.sri ganganagar this is my home twon.sri ganganagar this is my home twon.  ";

            annotationView.detailCalloutAccessoryView = subTitlelbl;

            NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:150];

             NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:subTitlelbl attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationGreaterThanOrEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1 constant:0];
            [subTitlelbl setNumberOfLines:0];
            [subTitlelbl addConstraint:width];
            [subTitlelbl addConstraint:height];




            return annotationView;
        } else
            return nil;
    }

enter image description here

关于iphone - 如何显示 MKAnnotation 的副标题 2 行文本并更改右侧按钮的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5831382/

相关文章:

iOS:获取位置之间的距离

ios - 如何轻轻地从 map View 中删除注释?

ios - 标注按钮使应用程序崩溃 - MapKit

ios - 在注释数组上设置公开按钮

iphone - 如何在核心数据中使用 NSPredicate 从属性之一获取值

iphone - 在相机 View 上叠加一个框架,然后保存并使用生成的照片(捕获的内容和叠加框架)

iphone - viewWillAppear 时 ECSlidingViewController 错误(对象不能为 nil)

customtableview单元格中的iOS swift mapview

ios - 将 map 中的注释添加到 NSMutableArray

iphone - 训练 tesseract 与 iPhone 一起使用