write text in drawRect MKPolygonRenderer

[亡魂溺海] 提交于 2019-12-11 10:18:17

问题


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).

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.

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()
}

回答1:


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

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.

Example:

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();
}

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



来源:https://stackoverflow.com/questions/35492876/write-text-in-drawrect-mkpolygonrenderer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!