有没有办法使用Paths Drawing添加文本 [英] Is there a way to add text using Paths Drawing

查看:106
本文介绍了有没有办法使用Paths Drawing添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从MKOverlayPathView继承的地图自定义视图。我需要这个自定义视图来显示圆,线和文本。

I have a map custom view that inherit from MKOverlayPathView. I need this custom view to display circle, line and text.

我已经设法使用路径绘制CGPathAddArc和CGPathAddLineToPoint函数绘制圆和线。

I already managed to draw circle and line using path drawing CGPathAddArc and CGPathAddLineToPoint functions.

但是,我仍然需要添加文字。

However, I still need to add text.

我尝试使用

 [text drawAtPoint:centerPoint withFont:font];

但我的上下文错误无效。

but I got invalid context error.

任何想法?

推荐答案

使用 MKOverlayPathView ,我认为最简单的方法添加文本是为了覆盖 drawMapRect:zoomScale:inContext:并将路径和文本绘图放在那里(并且不执行任何操作或不实现 createPath )。

With MKOverlayPathView, I think the easiest way to add text is to override drawMapRect:zoomScale:inContext: and put the path and text drawing there (and do nothing in or don't implement createPath).

但是如果你打算使用 drawMapRect ,你可能只想切换到普通的子类 MKOverlayView 而不是 MKOverlayPathView

But if you're going to use drawMapRect anyway, you might want to just switch to subclassing a plain MKOverlayView instead of MKOverlayPathView.

MKOverlayView ,覆盖 drawMapRect:zoomScale:inContext:方法并使用 CGContextAddArc (或 CGContextAddEllipseInRect CGPathAddArc )。

With an MKOverlayView, override the drawMapRect:zoomScale:inContext: method and draw the circle using CGContextAddArc (or CGContextAddEllipseInRect or CGPathAddArc).

您可以在此方法中使用 drawAtPoint 绘制文本,该方法将具有所需的上下文

You can draw the text using drawAtPoint in this method which will have the required context.

例如:

-(void)drawMapRect:(MKMapRect)mapRect zoomScale:(MKZoomScale)zoomScale inContext:(CGContextRef)context
{
    //calculate CG values from circle coordinate and radius...
    CLLocationCoordinate2D center = circle_overlay_center_coordinate_here;

    CGPoint centerPoint = 
        [self pointForMapPoint:MKMapPointForCoordinate(center)];

    CGFloat radius = MKMapPointsPerMeterAtLatitude(center.latitude) * 
                         circle_overlay_radius_here;

    CGFloat roadWidth = MKRoadWidthAtZoomScale(zoomScale);

    //draw the circle...
    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
    CGContextSetFillColorWithColor(context, [[UIColor blueColor] colorWithAlphaComponent:0.2].CGColor);
    CGContextSetLineWidth(context, roadWidth);
    CGContextAddArc(context, centerPoint.x, centerPoint.y, radius, 0, 2 * M_PI, true);
    CGContextDrawPath(context, kCGPathFillStroke);

    //draw the text...
    NSString *text = @"Hello";
    UIGraphicsPushContext(context);
    [[UIColor redColor] set];
    [text drawAtPoint:centerPoint 
             withFont:[UIFont systemFontOfSize:(5.0 * roadWidth)]];
    UIGraphicsPopContext();
}

关于另一个答案中的评论......

In relation to a comment in another answer...

当关联的 MKOverlay 的中心坐标或半径(或其他)发生变化时,您可以制作 MKOverlayView 通过在其上调用 setNeedsDisplayInMapRect:来移动(而不是再次删除和添加叠加层)。 (当使用 MKOverlayPathView 时,您可以调用 invalidatePath 。)

When the center coordinate or radius (or whatever) of the associated MKOverlay changes, you can make the MKOverlayView "move" by calling setNeedsDisplayInMapRect: on it (instead of removing and adding the overlay again). (When using a MKOverlayPathView, you can call invalidatePath instead.)

调用 setNeedsDisplayInMapRect:时,可以为map rect参数传递overlay的 boundingMapRect

When calling setNeedsDisplayInMapRect:, you can pass the boundingMapRect of the overlay for the map rect parameter.

在WWDC 2010的LocationReminders示例应用中,叠加视图使用KVO观察对相关 MKOverlay 的更改,只要它检测到圆圈属性的更改,它就会自动移动,但您可以通过其他方式监视更改,并从叠加视图外部显式调用 setNeedsDisplayInMapRect:

In the LocationReminders sample app from WWDC 2010, the overlay view uses KVO to observe changes to the associated MKOverlay and makes itself move whenever it detects a change to the circle's properties but you could monitor the changes in other ways and call setNeedsDisplayInMapRect: explicitly from outside the overlay view.


(在我使用 MKOverlayPathView 提到的另一个答案的评论中,这就是LocationReminders应用程序实现的方式一个移动的圆圈叠加视图。但是我应该提到你怎么也可以用 MKOverlayView 画一个圆圈。很抱歉。)

(In a comment on another answer I did mention using MKOverlayPathView and that is how the LocationReminders app implements a moving circle overlay view. But I should have mentioned how you can also use MKOverlayView to draw a circle. Sorry about that.)

这篇关于有没有办法使用Paths Drawing添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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