【问题标题】:Getting an MKAnnotationView subclass with a dynamically drawn pin使用动态绘制的 pin 获取 MKAnnotationView 子类
【发布时间】: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


    【解决方案1】:

    就在调用drawAtPoint 之前,设置您想要的文本颜色,否则它使用上面CGContextSetRGBFillColor 调用设置的颜色:

    [[UIColor blackColor] set];  //or some color that contrasts with background
    [label drawAtPoint:labelLocation withFont:font];
    

    如果在圆圈之后绘制文字,它应该出现在圆圈上方,反之亦然。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-25
      相关资源
      最近更新 更多