在贝塞尔曲线路径上绘制文本

时间:2018-08-09 11:52:48

标签: ios swift draw uibezierpath

我想用CATextLayer在贝塞尔曲线上绘制文本,但是不知道绘制的确切过程。我有示例代码来画线,并沿着那条线我想画文本,但这不能正常工作。所以请帮助我。

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

0 个答案:

没有答案