获取带有动态绘制引脚的 MKAnnotationView 子类
我有一个代表地图引脚簇的 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 的值,我将根据空间大小进行调整每个字母都占据,以便数字居中。我现在得到的是一个半透明的灰色圆圈,但没有文字。我认为文本绘制在错误的位置。另外,如何指定文本应出现在圆圈前面而不是后面?
I've got an MKAnnotation subclass representing a cluster of map pins, with the number of pins in the cluster retrievable from the MKAnnotation subclass. For these annotations, I would like to display a grey circle with a black bold number representing the number of pins in the cluster. I've made an MKAnnotationView subclass which implements initWithAnnotation:reuseIdentifier and drawRect: methods, and this is my implementation of the drawRect: method:
- (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);
}
Ignore the values for labelLocation in the if statement, I'm going to adjust that according to how much space each letter takes up so that the number is centered. What I am getting at the moment is a translucent grey circle, but no text. I assume the text is being drawn in the wrong location. Also how do I specify that the text should appear in front of the circle rather than behind it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在调用
drawAtPoint
之前,设置您想要文本的颜色,否则它将使用上面的CGContextSetRGBFillColor
调用设置的颜色:如果您在圆圈之后绘制文本,它会使用应出现在圆圈上方,反之亦然。
Right before calling
drawAtPoint
, set the color that you want the text in otherwise it uses the color set by theCGContextSetRGBFillColor
call above:If you draw the text after the circle, it should appear above the circle and vice versa.