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.
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:
PS: Don't look at this answer on a CRT.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With