问题描述
我有一个带有字符串'LA'的UILabel
.我在分配给其string
属性的NSAttributedString
中也有一个具有相同字符的CATextLayer
. UILabel
中的字距调整与CATextLayer
中的字距明显不同.这是代码.
I have a UILabel
with the string 'LA'. I also have a CATextLayer
with the same characters in an NSAttributedString
assigned to its string
property. The kerning in the UILabel
is noticeably different from the CATextLayer
. Here's the code.
- (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];
}
这是结果的图像.
这两种方法的字距调整为何不同?在CATextLayer
示例中我在做什么错了?
Why is the kerning different between these two methods and what am I doing wrong in the CATextLayer
example?
推荐答案
UIKit通常使用WebKit进行文本呈现(如,最有可能是出于性能方面的原因.如果您确实需要超高精度,那么可以使用自定义UILabel
重新实现 c8>作为其后端.
UIKit generally uses WebKit for its text rendering (as visible in this crash log), most likely for performance reasons. If you really need super-precision then there are some custom UILabel
reimplementations using CoreText
as its back-end.
从iOS7开始,这不再成立,因为UILabel
也使用基于CoreText的TextKit进行渲染.
As of iOS7 this is no longer true since UILabel
uses TextKit for its rendering which is based on CoreText as well.
这篇关于UILabel默认字距调整与CATextLayer不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!