How to get text in a CATextLayer to be clear

前端 未结 7 1946
不思量自难忘°
不思量自难忘° 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:03

    Before setting shouldRasterize, you should:

    1. set the rasterizationScale of the base layer you are going to rasterize
    2. set the contentsScale property of any CATextLayers and possibly other types of layers(it never hurts to do it)

    If you don't do #1, then the retina version of sub layers will look blurry, even for normal CALayers.

    - (void)viewDidLoad {
      [super viewDidLoad];
    
      CALayer *mainLayer = [[self view] layer];
      [mainLayer setRasterizationScale:[[UIScreen mainScreen] scale]];
    
      CATextLayer *messageLayer = [CATextLayer layer];
      [messageLayer setForegroundColor:[[UIColor blackColor] CGColor]];
      [messageLayer setContentsScale:[[UIScreen mainScreen] scale]];
      [messageLayer setFrame:CGRectMake(50, 170, 250, 50)];
      [messageLayer setString:(id)@"asdfasd"];
    
      [mainLayer addSublayer:messageLayer];
    
      [mainLayer setShouldRasterize:YES];
    }
    

提交回复
热议问题