根据文档,可以在 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.
以下是我对这句话的理解:
@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;
这是行不通的。不使用亚像素抗锯齿绘制文本。