0

我正在尝试让 MKAnnotation 字幕具有彩色文本,但我无法使用 NSMutableAttributedString 实现这一点。

有谁知道如何实现这一目标?

我当前的代码:

customAnn.h

interface MapAnnotation : NSObject <MKAnnotation>

@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end

customAnn.m

@implementation MapAnnotation

- (NSString *)title
{
    return @"Some Title";
}

- (NSString *)subtitle
{
    NSMutableAttributedString *subPart = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@ ", [[[data valueForKey:@"16"] sortedArrayUsingSelector: @selector(localizedStandardCompare:)] componentsJoinedByString:@" "]]];
    [subPart addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:20/225.0 green:30/255.0 blue:15/255.0 alpha:1] range:NSMakeRange(0, subPart.length)];
    return subPart;
}
@end

目前,代码无法正常工作,因为- (NSString *)subtitle想要NSString但得到NSMutableAttributedString- 它崩溃了,但如果更改为- (NSMutableAttributedString *)subtitle我什至无法编译。

我没有其他想法如何实现这一点。

谢谢。

4

1 回答 1

0

我认为您无法使用股票 MKAnnotation 做到这一点。我会创建自己的自定义注释视图,这样您就可以根据需要自定义视图。

这是一个很好的教程:

http://blog.asynchrony.com/2010/09/building-custom-map-annotation-callouts-part-1/

于 2014-05-20T17:26:26.400 回答