在圆形叠加层中绘制文本
我正在尝试在 MKMapView 上绘制一些包含文本的圆形叠加层。 我对 MKCircleView 进行了子类化,在其中放置了以下内容(基于 this ),但文本不出现。圆圈正确显示。 (也尝试了第一个响应的解决方案,结果相同)。
-(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
它们正常吗?我缺少什么?
谢谢。
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 not appear. The circles show up correctly. (Also tried the first response's solution, same result).
-(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();
}
When debugging, I get values like these
MKC : 43253760.000000, 104071168.000000 ----> 1.776503 , 1.999245
MKC : 43253760.000000, 104071168.000000 ----> -1.562442 , -2.043090
Are they normal ? What am I missing ?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我相信您的代码正在工作,问题是文本没有正确缩放,使其不可见。
使用
MKRoadWidthAtZoomScale
函数根据zoomScale
缩放字体大小:另请确保使用与底层圆圈颜色不同的文本颜色。
请注意,使用
drawInRect
将导致文本被限制在圆圈内,并且可能会被截断。如果您想始终显示所有文本,可以使用drawAtPoint
代替。I believe your code is working and the problem is that the text is not being scaled properly making it invisible.
Scale the font size based on the
zoomScale
using theMKRoadWidthAtZoomScale
function:Also be sure to use a text color that's different from the underlying circle's color.
Note that using
drawInRect
will result in the text being restricted to inside the circle and may get truncated. If you want to always show all the text, you could usedrawAtPoint
instead.结合这里的答案并更新 IOS7:
Combining the answers here and updating for IOS7:
您的文本很可能被绘制到不可见的矩形中。
我要做的第一件事是尝试将值打印为 %f 而不是 %lf,因为这些值看起来很疯狂。您还应该打印出两个矩形的
.size.width
和.size.height
(mapRect
和overallCGRect
>)。如果这不能引导您进行合理的矩形定义,请尝试自己定义 CGRect,如 CGRectMake(0,0,100,20) 并查看文本是否绘制。
您还可以尝试简单地将一个填充矩形绘制到与您绘制文本的相同的
overallCGRect
上。It is most likely that your text is drawn to a rectangle which is not visible.
First thing I would do is try printing the values as %f instead of %lf, as those values look crazy. You should also print out the
.size.width
and.size.height
for the two rects (mapRect
andoverallCGRect
).If that doesn't lead you to a sensible rectangle definition, then try defining a CGRect yourself like
CGRectMake(0,0,100,20)
and see if the text draws.You can also try simply drawing a filled rectangle to the same
overallCGRect
that you are drawing your text into.这是 macOS 的 Swift 版本 - 使用自定义 MKOverlay 和 MKOverlayRenderer
Here is a Swift version for macOS - makes use of a custom MKOverlay and MKOverlayRenderer