CATextLayer不能与swift中的UILabel文本水平居中对齐



当我在swift 5中使用CATextLayer时,我使用水平对齐模式将其对齐到中心,如下所示但是它不能对准中心。这是我的一些代码

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

但当我在ipad pro上运行时,我将位置x设置为500,它移动到的中心

textLayer.position = CGPoint(500,20)

但它并不准确,我也不知道为什么。那么,如何将CATextLayer与中心对齐,作为中心标签文本呢?

我不知道你的代码中什么是textLayer,但如果你想使用CATextLayerAlignmentMode,你可以将其设置为你的标签

myLabel.textAlignment = CATextLayerAlignmentMode.center

最新更新