Center UILabel inside of a CAShapeLayer

问题内容:

I need to center an UILabel inside of a circular CAShapeLayer and I don’t know how to do it. Besides, I would like to know whether it’s strictly necessary using a CATextLayer.

问题评论:

1
An alternative approach would be to subclass UILabel and add a CAShapeLayer as a sublayer. Then override any methods calculating the total frame (such as intrinsicContentSize). This would have the benefit to treat it as one object layout(and other)-wise.
Isn’t there any way to get the X and Y of the CAShapeLayer ? If so, I could set the UILabel in that right place.
– Javier Sanz Rozalén
6 hours ago
Of course there is. There is a frame property.
I am trying to center it using its frame (using x and y parameters) but the label just goes to the top of the view. Any idea ?
– Javier Sanz Rozalén
4 hours ago
Keep in mind that: 1. Frame is always relative to the super(view | layer) 2. Frame’s origin in iOS identifies the top-left corner

原文地址:

https://stackoverflow.com/questions/47750282/center-uilabel-inside-of-a-cashapelayer

添加评论

友情链接:蝴蝶教程