How to make CATextLayer smooth text?

According to the documentation , you can 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 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 does not work. Text is not drawn using sub-pixel smoothing.

+8
objective-c core-animation antialiasing macos subpixel
source share
5 answers

The method is described below:

Case 2: If you use CATextLayer directly, you will 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]; } 

The following is a comparison of smoothed and 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.

+8
source share

May be late. But I see that this question has the perfect solution, as Steve says, and nacho4d comment:

How to get text in CATextLayer to be clear

+3
source share

As I understand this documentation, it says that CATextLayer always disables sub-pixel anti-aliasing. The sentence you are quoting is simply given as an explanation for this, and not as instructions on how to include it - it is followed by:

Setting the opacity property of the YES layer does not change the rendering mode.

... which implies that even if you use an opaque background for the layer, this will not change the fact that CATextLayer does not use sub-pixel-aa.

+2
source share

If you are faced with finding a problem when CATextLayer renders slightly blurry text in OSX, after a lot of bumps on the wall, I got clear text by doing the following:

 text_layer.contentsScale = self.window!.backingScaleFactor text_layer.zPosition = 0 

(I also establish that the contents of the ViewsScale background layer are the same).

+1
source share

The accepted answer does not work for mine.

I found a working solution on the Ignacio Nieto Blog

 textLayer.contentsScale = UIScreen.mainScreen().scale 
0
source share

All Articles