CATextLayer cannot align to center horizontally with UILabel text in swift

987 Views Asked by At

When i use CATextLayer in swift 5, i align it to center by using Horizontal alignment modes follow here but it cannot align to center. Here is some of my code

myLabel.textAlignment = .center

textLayer.frame = myLabel.bounds;
textLayer.alignmentMode = CATextLayerAlignmentMode.center

But when i run on ipad pro, i set position x to 500 and it moved to center

textLayer.position = CGPoint(500,20)

But it's not exact and i don't know the reason why it is. So how can i align the CATextLayer exactly to the center as the center Label text?

1

There are 1 best solutions below

1
Masoud Roosta On

I don't know what is textLayer in your code but if you want use CATextLayerAlignmentMode you can set it to your label

  myLabel.textAlignment = CATextLayerAlignmentMode.center