以圆形覆盖绘制文本

Tem*_*lar 10 iphone overlay objective-c mkmapview mkoverlay

我正在尝试在MKMapView上绘制一些包含文本的圆形叠加层.我已经将MKCircleView子类化了,其中我放了以下内容(基于),但文本没有出现.圆圈正确显示.(也尝试了第一个响应的解决方案,结果相同).

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context {
   [super drawMapRect:mapRect zoomScale:zoomScale inContext:context];
   NSString * t= @"XXXXX\nXXXX" ;
   UIGraphicsPushContext(context);
   CGContextSaveGState(context); 
   [[UIColor redColor] set];
   CGRect overallCGRect = [self rectForMapRect:[self.overlay boundingMapRect]];
   NSLog(@"MKC :  %lf, %lf ----> %lf , %lf ", mapRect.origin.x ,mapRect.origin.y , overallCGRect.origin.x, overallCGRect.origin.y);
   [t drawInRect:overallCGRect withFont:[UIFont fontWithName:@"Arial" size:10.0] lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
   CGContextRestoreGState(context);
   UIGraphicsPopContext();
}
Run Code Online (Sandbox Code Playgroud)

调试时,我得到这些值

MKC :  43253760.000000, 104071168.000000 ----> 1.776503 , 1.999245 
MKC :  43253760.000000, 104071168.000000 ----> -1.562442 , -2.043090
Run Code Online (Sandbox Code Playgroud)

他们是正常的吗?我错过了什么?

谢谢.

小智 12

我相信你的代码是有效的,问题是文本没有正确缩放,使其无形.

根据zoomScale使用MKRoadWidthAtZoomScale函数缩放字体大小:

[t drawInRect:overallCGRect withFont:[UIFont fontWithName:@"Arial" 
    size:(10.0 * MKRoadWidthAtZoomScale(zoomScale))] 
    lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
Run Code Online (Sandbox Code Playgroud)

另外一定要使用与底层圆圈颜色不同的文字颜色.

请注意,使用drawInRect将导致文本被限制在圆圈内并可能被截断.如果您想要始终显示所有文本,则可以使用drawAtPoint.