Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does drawing with CoreText look different than UILabel?

I'm using TTTAttributedLabel (which uses CoreText) instead of a UILabel to be able to bold certain parts. It works great but the text doesn't look the same. It looks like it's using a different font. I've set it to be the same font (Helvetica) but one is a CTFont and one is a UIFont. Why do they look different?

  • Here's the UIFont for the UILabel: [UIFont systemFontOfSize:15]
  • Here's the CTFont for CoreText: CTFontCreateWithName((__bridge CFStringRef)[UIFont systemFontOfSize:15].fontName, 15, NULL)

UILabel Screenshot:

enter image description here

CoreText Screenshot:

enter image description here

The 'p' and the 'o' in "promenade" is the easiest part to see the font doesn't look the same. Letters are more round in the CoreText version.

like image 384
jasongregori Avatar asked Nov 16 '11 20:11

jasongregori


2 Answers

UILabel uses WebKit style text-rendering that takes shortcuts in accuracy to improve drawing performance. CoreText (originally from OS X) takes the opposite, render exactly and as perfectly as possible at the expense of performance.

For iOS apps, reserve CoreText for only the most visibly critical UI (such as a headline) and where you can potentially cache the rendered output.

like image 149
jarjar Avatar answered Nov 18 '22 17:11

jarjar


I spent a while trying to solve this problem too, and I think I've got it. From what I've tested so far, your CoreText rendering will look identical to the rendering you see on UILabel, UITextView, etc, by setting kerning to zero:

NSNumber *kern = [NSNumber numberWithFloat:0];
NSRange full = NSMakeRange(0, [attributedText string].length);
[attributedText addAttribute:(id)kCTKernAttributeName value:kern range:full];

This is really strange behavior, and I can't say I know why it's happening, but this is what Apple seems to be doing. (It actually increased spacing in some places, like in between 'Te'.)

Tested on iOS 5.1.

like image 8
Dan Avatar answered Nov 18 '22 18:11

Dan