Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have CATextLayer smooth its text?

According to the documentation, it is possible to enable font smoothing inside CATextLayer:

Text can only be drawn using sub-pixel antialiasing when it is composited into an existing opaque background at the same time that it's rasterized.

Here is how I understand this sentence:

@implementation CATextLayerWithFontSmoothing
-(id)init {
    self=[super init];
    if (self) {
    CALayer * whiteBackground = [CALayer layer];
    CATextLayer * blackText = [CATextLayer layer];

    [whiteBackground setBounds:NSMakeRect(0, 0, 300, 300)];
    [blackText setBounds:NSMakeRect(0, 0, 300, 300)];

    [whiteBackground setBackgroundColor:[NSColor whiteColor].CGColor];
    [blackText setForegroundColor:[NSColor blackColor].CGColor];
    [blackText setString:@"CATextLayer"];

    [blackText setShouldRasterize:YES];

    [self addSublayer:whiteBackground];
    [self addSublayer: blackText];

    }
    return self;

which doesn't work. Text is not drawn using subpixel anti-aliasing.

like image 618
alecail Avatar asked Feb 10 '13 10:02

alecail


Video Answer


1 Answers

The method described here works:

Case 2: If you're using a CATextLayer directly, you'll need to subclass CATextLayer and do something like the following in your drawing code:

- (void)drawInContext:(CGContextRef)ctx
{
CGContextSetRGBFillColor (ctx, r, g, b, a);
CGContextFillRect (ctx, [self bounds]);
CGContextSetShouldSmoothFonts (ctx, true);
[super drawInContext:ctx];
}

Here is a comparison of smoothed vs non-smoothed:

text on a regular CATextLayer

text with sub-pixel-aa

800% zoom of both layers

PS: Don't look at this answer on a CRT.

like image 177
alecail Avatar answered Oct 03 '22 23:10

alecail