在drawRect MKPolygonRenderer中写入文本 [英] write text in drawRect MKPolygonRenderer

查看:121
本文介绍了在drawRect MKPolygonRenderer中写入文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我机智了.我是MapKit的新手,并编写了一个健康统计应用程序,该程序在MKMapView上绘制相对于患者居住地的数据.我可以放下注释,并毫无问题地绘制邮政编码区域的多边形.

Ok I'm at my wits end. I'm new to MapKit and writing a health stats app that plots data on a MKMapView relative to where patients live. I can drop annotations, and draw polygons of postcode areas with no problem.

但是我想在地图多边形上写文本(而不是注释).

However I'd like to write text on the map polygon (and not an annotation).

这是我的代码,我将MKPolygonRender子类化以访问drawRect.

Here's my code, I've subclassed MKPolygonRender to access drawRect.

多边形是完美的,但没有文字.我已经尝试过写入多边形的第一个点(最终我会找到中心)

Polygons are perfect but no text. I've tried write to the first point in the polygon (I will find centre eventually)

一直呆了几个晚上,真是感激不尽.

Been stuck for several nights so really grateful.

PolygonRender类:MKPolygonRenderer { var point:CGPoint!

class PolygonRender: MKPolygonRenderer { var point:CGPoint!

override init(overlay: MKOverlay) {
    super.init(overlay: overlay)
}

override func drawMapRect(mapRect: MKMapRect, zoomScale: MKZoomScale, inContext context: CGContext) {


    super.drawMapRect(mapRect, zoomScale: zoomScale, inContext: context)
    let mapRect:MKMapRect = self.overlay.boundingMapRect
    let rect:CGRect = self.rectForMapRect(mapRect)
    UIGraphicsPushContext(context)
    var bPath = UIBezierPath()
    //let polygon:MKPolygon = self.polygon
    super.fillColor = UIColor.yellowColor()
    let points = self.polygon.points()
    let pointCount = self.polygon.pointCount

    var point:CGPoint = self.pointForMapPoint(points[0])
    bPath.moveToPoint(point)

    for var i = 1; i < pointCount; i++ {
        point = self.pointForMapPoint(points[i])
        bPath.addLineToPoint(point)

    }
    bPath.closePath()
    bPath.addClip()

    let roadWidth:CGFloat = MKRoadWidthAtZoomScale(zoomScale)

    let attributes: [String: AnyObject] = [
        NSForegroundColorAttributeName : UIColor.blackColor(),
        NSFontAttributeName : UIFont.systemFontOfSize(5 * roadWidth)]


    let centerPoint = pointForMapPoint(points[0])
    print(centerPoint)
    let string:NSString = "Hello world"
    string.drawAtPoint(centerPoint, withAttributes: attributes)




    UIGraphicsPopContext()
}

推荐答案

通常来说,您做得对,但我认为字体太小,看不到文本.

Generally you do it right but I think the font size is too small to see the text.

尝试将字体大小设置为100或更大的值,以找到其中的文本,但是您需要放大.

Try to set font size to value like 100 or bigger to find out the text is there, but you will need to zoom in.

示例:

public override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {

    let rect:CGRect = self.rect(for: mapRect)

    UIGraphicsPushContext(context);

    // ...

    UIGraphicsPushContext(context);
    UIColor.black.setStroke()
    context.setLineWidth(1)
    context.stroke(rect.insetBy(dx: 0.5, dy: 0.5))

    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.alignment = .center

    let attributes = [NSAttributedStringKey.paragraphStyle  : paragraphStyle,
                      NSAttributedStringKey.font            : UIFont.systemFont(ofSize: 100.0),
                      NSAttributedStringKey.foregroundColor : UIColor.black]
    "\(rect.size)".draw(in: rect, withAttributes: attributes)
    UIGraphicsPopContext();
}

下图显示了当rect大小为1024x1024时,如何在地图上显示字体大小为100的文本

Image below shows how on the map is displayed text with font size 100 when the rect size is 1024x1024

这篇关于在drawRect MKPolygonRenderer中写入文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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