我有一个带有字符串“LA”的UILabel。在分配给CATextLayer属性的NSAttributedString中,我也有一个带有相同字符的stringUILabel的字距调整与CATextLayer明显不同。这是代码。

- (void)viewDidLoad
{
    [super viewDidLoad];

    //
    // UILabel
    //
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 280, 100)];
    label1.text = @"LA";
    label1.backgroundColor = [UIColor clearColor];
    label1.font = [UIFont fontWithName:@"Futura" size:90.0];
    [self.view addSubview:label1];

    //
    // CATextLayer
    //
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 130, 280, 100)];
    label2.backgroundColor = [UIColor clearColor];
    CATextLayer *textLayer = [[CATextLayer alloc] init];
    textLayer.frame = label2.layer.bounds;
    textLayer.contentsScale = [[UIScreen mainScreen] scale];
    [label2.layer addSublayer:textLayer];
    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"LA"];
    CTFontRef aFont = CTFontCreateWithName((__bridge CFStringRef)@"Futura", 90.0, NULL);
    [string addAttribute:(NSString*)kCTFontAttributeName value:(__bridge id)aFont range:NSMakeRange(0, [string length])];
    textLayer.string = string;
    [self.view addSubview:label2];
}

这是结果的an image

为什么这两种方法的字距调整不同?在CATextLayer示例中我在做什么错?

最佳答案

UIKit通常使用WebKit进行文本渲染(在this crash log)中可见,最有可能出于性能原因。如果您确实需要超高精度,则有些custom UILabel reimplementations使用CoreText作为其后端。

编辑:
从iOS7开始,这不再是正确的,因为UILabel也使用TextKit进行渲染,该渲染也基于CoreText。

10-08 07:28
查看更多