I am trying to make a CAShapeLayer
with a sublayer of CATextLayer
to follow a UIBezierPath
.
I am able to draw the CAShapeLayer
using UIBezierPath
but CATextLayer
doesn't follow the same path. Here is what I did so far.
// 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)