How to get text in a CATextLayer to be clear

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

    You should do 2 things, the first was mentioned above:

    Extend CATextLayer and set the opaque and contentsScale properties to properly support retina display, then render with anti aliasing enabled for text.

    + (TextActionLayer*) layer
    {
      TextActionLayer *layer = [[TextActionLayer alloc] init];
      layer.opaque = TRUE;
      CGFloat scale = [[UIScreen mainScreen] scale];
      layer.contentsScale = scale;
      return [layer autorelease];
    }
    
    // Render after enabling with anti aliasing for text
    
    - (void)drawInContext:(CGContextRef)ctx
    {
        CGRect bounds = self.bounds;
        CGContextSetFillColorWithColor(ctx, self.backgroundColor);
        CGContextFillRect(ctx, bounds);
        CGContextSetShouldSmoothFonts(ctx, TRUE);
        [super drawInContext:ctx];
    }
    

提交回复
热议问题