jasongregori
jasongregori

Reputation: 11646

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?

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.

Upvotes: 8

Views: 2972

Answers (3)

jarjar
jarjar

Reputation: 1701

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.

Upvotes: 9

Dan
Dan

Reputation: 133

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.

Upvotes: 8

Adam Ernst
Adam Ernst

Reputation: 54040

I'm pretty sure it's because Core Text has more advanced rendering (kerning and spacing improvements, and so forth). I'm sorry I can't dig up any docs to prove this, but it's the general feel I get from it.

The bottom line is, you can't count on getting the same measurements between Core Text and UILabel and the other UIKit APIs. They use different layout and spacing code.

Upvotes: 3

Related Questions