我正在尝试在 MKMapView 上绘制一些包含文本的圆形叠加层。
我对 MKCircleView 进行了子类化,在其中放置了以下内容(基于this https://stackoverflow.com/questions/4165842/draw-simple-text-in-a-mkpolygonview),但文本不出现。圆圈正确显示。 (也尝试了第一个响应的解决方案,结果相同)。
-(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();
}
调试时,我得到这样的值
MKC : 43253760.000000, 104071168.000000 ----> 1.776503 , 1.999245
MKC : 43253760.000000, 104071168.000000 ----> -1.562442 , -2.043090
他们正常吗?我缺少什么?
Thanks.
我相信您的代码正在工作,问题是文本没有正确缩放,使其不可见。
根据缩放字体大小zoomScale
使用MKRoadWidthAtZoomScale
功能:
[t drawInRect:overallCGRect withFont:[UIFont fontWithName:@"Arial"
size:(10.0 * MKRoadWidthAtZoomScale(zoomScale))]
lineBreakMode:UILineBreakModeClip alignment:UITextAlignmentCenter];
还要确保使用与底层圆圈颜色不同的文本颜色。
请注意,使用drawInRect
将导致文本被限制在圆圈内并可能被截断。如果您想始终显示所有文本,您可以使用drawAtPoint
反而。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)