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];
}
Here's an image of the results.
Why is the kerning different between these two methods and what am I doing wrong in the CATextLayer
example?
Well
Core Text
is really different when compared to drawing strings using UIKit, probably because it comes fromCore Foundation
and notAppKit
orUIKit
. I do understand your requirements to use a label for doing the metrics hard job on a string. The only solution for me is to match the kerning ofUILabel
in the attributed string, unfortunately I don't know the exact value but you can use this property to change that valuekCTKernAttributeName
. You should pay attention also for the interline that could be not the same.Forcing that value to the matching kerning you could have the correct behavior. If you want the opposite (match CT kerning) you should do some math an later apply to the label a
UIEdgeInset
to math the correct label.Hope this helps.
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 usingCoreText
as its back-end.EDIT: As of iOS7 this is no longer true since
UILabel
uses TextKit for its rendering which is based on CoreText as well.you should add attribute to your NSMutableAttributedString.
For the kerning:
If you also need the line spacing, or set LineBreadMode:
At the end, may you need calculate the number of line(linenum) about your kerning,line spacing and LineBreakMode: