Mic*_*ton 12 iphone uikit core-text ios
我有一个UILabel字符串'LA'.我也有一个与其属性分配CATextLayer相同的字符.字距中的字距显着不同于.这是代码.NSAttributedStringstringUILabelCATextLayer
- (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];
}
Run Code Online (Sandbox Code Playgroud)
这是结果的图像.
为什么这两种方法之间的字距不同以及我在这个CATextLayer例子中做错了什么?
UIKit 通常使用 WebKit 进行文本渲染(如崩溃日志中所示),很可能是出于性能原因。如果您确实需要超精度,那么可以使用一些自定义UILabel重新实现CoreText作为其后端。
编辑:
从 iOS7 开始,情况不再如此,因为UILabel使用 TextKit 进行渲染,而它也基于 CoreText。
| 归档时间: |
|
| 查看次数: |
1688 次 |
| 最近记录: |