如何使CATextLayer遵循为形状定义的相同BeizerPath

时间:2018-01-10 05:46:19

标签: ios swift uibezierpath catextlayer

我正在尝试制作CAShapeLayer

CATextLayer的子图层跟随UIBezierPath

我可以使用CAShapeLayer绘制UIBezierPath,但CATextLayer并未遵循相同的路径。这是我到目前为止所做的。

        // Define a bezier path

        let bezierPath = UIBezierPath.init()
        let origin = CGPoint.init(x: containerView.frame.size.width-75, y: 0)
        let firstPoint  = CGPoint.init(x: origin.x+34, y: origin.y)
        let secondPoint = CGPoint.init(x: containerView.frame.size.width, y: origin.y+25)
        let thirdPoint  = CGPoint.init(x: secondPoint.x, y: secondPoint.y+25)
        bezierPath.move(to: origin)
        bezierPath.addLine(to: firstPoint)
        bezierPath.addLine(to: secondPoint)
        bezierPath.addLine(to: thirdPoint)
        bezierPath.addLine(to: origin)
        bezierPath.close()

        // Add a shape to follow the defined Bezier Path

        let shape = CAShapeLayer.init()
        shape.path  = bezierPath.cgPath
        shape.fillColor = UIColor.yellow.cgColor
        containerView.layer.addSublayer(shape)

        // Add a text layer

        let textlayer = CATextLayer.init()
        textlayer.string = "iPhone X"
        textlayer.fontSize = 12
        textlayer.frame = bezierPath.bounds
        shape.addSublayer(textlayer)

This is what I got as result, the text should follow the same path

0 个答案:

没有答案