gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 18:23:29 27 4
gpt4 key购买 nike

我正在查看 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com