How to get text in a CATextLayer to be clear

前端 未结 7 1820
不思量自难忘°
不思量自难忘° 2021-01-29 18:19

I\'ve made a CALayer with an added CATextLayer and the text comes out blurry. In the docs, they talk about \"sub-pixel antialiasing\", but that doesn\

相关标签:
7条回答
  • 2021-01-29 19:14

    Short answer — You need to set the contents scaling:

    textLayer.contentsScale = [[UIScreen mainScreen] scale];
    

    A while ago I learned that when you have custom drawing code, you have to check for the retina display and scale your graphics accordingly. UIKit takes care of most of this, including font scaling.

    Not so with CATextLayer.

    My blurriness came from having a .zPosition that was not zero, that is, I had a transform applied to my parent layer. By setting this to zero, the blurriness went away, and was replaced by serious pixelation.

    After searching high and low, I found that you can set .contentsScale for a CATextLayer and you can set it to [[UIScreen mainScreen] scale] to match the screen resolution. (I assume this works for non-retina, but I haven't checked - too tired)

    After including this for my CATextLayer the text became crisp. Note - it's not necessary for the parent layer.

    And the blurriness? It comes back when you're rotating in 3D, but you don't notice it because the text starts out clear and while it's in motion, you can't tell.

    Problem solved!

    0 讨论(0)
提交回复
热议问题