1

I want to draw text on Bezier path with CATextLayer but don't know the exact procedure to draw. I have sample code with me to draw the line and along that line I want to draw the text, and this is not working properly.

 func drawLineFromPoint(start : CGPoint, toPoint end:CGPoint, ofColor lineColor: UIColor, inView view:UIView) {
        let path = UIBezierPath()
        path.move(to: start)
        path.addLine(to: end)


        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        shapeLayer.strokeColor = lineColor.cgColor
        shapeLayer.lineWidth = 4.0

        let text = CATextLayer()
        text.string = "5 m"
        text.position  = CGPoint.init(x:shapeLayer.frame.midX , y: shapeLayer.frame.midY)
        let vert :CGFloat = (shapeLayer.frame.midX / shapeLayer.frame.midY)
        text.anchorPoint = CGPoint.init(x: 0.5, y: vert)
        text.fontSize = 25
        text.alignmentMode = kCAAlignmentCenter
        text.foregroundColor = UIColor.blue.cgColor

        shapeLayer.addSublayer(text)
        view.layer.addSublayer(shapeLayer)
    }
halfer
  • 19,824
  • 17
  • 99
  • 186
Yogesh Dalavi
  • 73
  • 4
  • 15

0 Answers0