How to add label or text in to CAShapeLayer
I guess you should add CATextLayer
as a sublayer to CALayer
... That works fine that way: try adding CAShapeLayer
first, and then CATextLayer
(to same CALayer
parent layer), for example in following order...
// assume self - is UIView instanceself.layer.addSublayer(shapedLayer) // shapedLayer - CAShapeLayer instanceself.layer.addSublayer(textLayer) // textLayer - CATextLayer instance
Swift 3.0
let label = UILabel()label.font = UIFont(name: "Helvetica-Bold", size: 12)label.frame = CGRect(x: OvalLayer.frame.origin.x + (circleWidth/2), y: OvalLayer.frame.origin.y, width: OvalLayer.bounds.width, height: OvalLayer.bounds.height)label.text = "Hello"label.textColor = UIColor.redlabel.isHidden = falseOvalLayer.addSublayer(label.layer)
You just need to get the center of your UIBezierPath
and add a label or a CATextLayer
to your current layer.
let center : CGPoint = CGPoint(x: CGRectGetMidX(ovalPathStart.bounds), y: CGRectGetMidX(ovalPathStart.bounds))
Now, create a UILabel
or CATextLayer
and set the center.