【发布时间】:2011-11-04 11:04:01
【问题描述】:
我有一个 MKAnnotation 子类,它表示一组地图引脚,集群中的引脚数可从 MKAnnotation 子类中检索。对于这些注释,我想显示一个带有黑色粗体数字的灰色圆圈,表示集群中的引脚数。我创建了一个 MKAnnotationView 子类,它实现了 initWithAnnotation:reuseIdentifier 和 drawRect: 方法,这是我对 drawRect: 方法的实现:
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(context, 0.7f, 0.7f, 0.7f, 0.5f);
CGContextFillEllipseInRect(context, rect);
UIFont *font = [UIFont boldSystemFontOfSize: [UIFont smallSystemFontSize]];
NSString *label = [NSString stringWithFormat:@"%i", [(LocationGroupAnnotation *)self.annotation locationCount]];
CGPoint labelLocation;
if ([label length] == 1)
{
labelLocation = CGPointMake(rect.size.width / 2.0f, (rect.size.height / 2.0f) - (font.capHeight / 2.0f));
} else if ([label length] == 2) {
labelLocation = CGPointMake(rect.size.width / 2.0f, (rect.size.height / 2.0f) - (font.capHeight / 2.0f));
} else {
labelLocation = CGPointMake(rect.size.width / 2.0f, (rect.size.height / 2.0f) - (font.capHeight / 2.0f));
}
[label drawAtPoint:labelLocation withFont:font];
NSLog(@"Drawn label at (%f,%f)", labelLocation.x, labelLocation.y);
}
忽略 if 语句中 labelLocation 的值,我将根据每个字母占用的空间进行调整,以使数字居中。我现在得到的是一个半透明的灰色圆圈,但没有文字。我假设文本被绘制在错误的位置。另外我如何指定文本应该出现在圆圈的前面而不是后面?
【问题讨论】:
标签: iphone objective-c mapkit mkannotationview