我正在尝试制作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)