以圆形覆盖绘制文本 [英] Draw text in circle overlay

查看:157
本文介绍了以圆形覆盖绘制文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

谢谢。

推荐答案

我相信您的代码正常运行问题是文本没有正确缩放使其不可见。

I believe your code is working and the problem is that the text is not being scaled properly making it invisible.

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

Scale the font size based on the zoomScale using the MKRoadWidthAtZoomScale function:

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

还要确保使用与底层圆圈颜色不同的文字颜色。

Also be sure to use a text color that's different from the underlying circle's color.

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

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 use drawAtPoint instead.

这篇关于以圆形覆盖绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆