获取带有动态绘制引脚的 MKAnnotationView 子类

发布于 2024-12-13 19:32:20 字数 1345 浏览 4 评论 0原文

我有一个代表地图引脚簇的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挽你眉间 2024-12-20 19:32:20

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

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

如果您在圆圈之后绘制文本,它会使用应出现在圆圈上方,反之亦然。

Right before calling drawAtPoint, set the color that you want the text in otherwise it uses the color set by the CGContextSetRGBFillColor call above:

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

If you draw the text after the circle, it should appear above the circle and vice versa.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文