Draw text in circle overlay

后端 未结 3 601
生来不讨喜
生来不讨喜 2020-11-30 11:23

I\'m trying to draw some circle overlays containing text on MKMapView. I have subclassed the MKCircleView, in which I put the following (based on this), but the text does no

3条回答
  •  情深已故
    2020-11-30 12:11

    Combining the answers here and updating for IOS7:

    -(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
    {
        [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
    
        UIGraphicsPushContext(context);
        CGContextSaveGState(context);
        [[UIColor blueColor] set];
    
        NSDictionary *fontAttributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10.0f * MKRoadWidthAtZoomScale(zoomScale)]};
        CGSize size = [[[self overlay] title] sizeWithAttributes:fontAttributes];
        CGFloat height = ceilf(size.height);
        CGFloat width  = ceilf(size.width);
    
        CGRect circleRect = [self rectForMapRect:[self.overlay boundingMapRect]];
        CGPoint center = CGPointMake(circleRect.origin.x + circleRect.size.width /2, circleRect.origin.y + circleRect.size.height /2);
        CGPoint textstart = CGPointMake(center.x - width/2, center.y - height /2 );
    
        [[[self overlay] title] drawAtPoint:textstart withAttributes:fontAttributes];
    
        CGContextRestoreGState(context);
        UIGraphicsPopContext();
    }
    

提交回复
热议问题