Font not kerning in iOS

2019-08-17 06:47发布

问题:

I've created a custom font using FontForge for use in my iOS app. When I use the font in my app, kerning is not being applied. There is a known bug that kerning is not being applied for UITextView. I'm using this in a UILabel and UITextField, so that's not the issue. I'm targeting iOS 5.0.

I'm able to view the font in Font Book and Pages and see that kerning is working. However, kerning doesn't work in Word for Mac. I have enabled kerning in Word, but it does not work there. I'm not concerned so much about Word. I primarily want this to work in iOS.

I've created the font as both .otf and .ttf fonts, with a variety of the FontForge options for generating the font set. Options I've played with are Apple, OpenType and OT/Old Style and OT/DSIG. Pages will properly apply kerning for all combinations of the font generation options, except when just OpenType is checked. Font Book will only show kerning when Apple is checked and nothing else. I've read some comments on other forums about getting fonts to work with Word, but not anything specific about kerning and iOS.

Anyone have any experience:

1) successfully creating a kerned font on OS X using FontForge for an iOS app?

OR

2) successfully creating a kerned font on OS X using FontForge that works in Word? This might get me closer, even if you've not had experience with fonts on iOS.

I have a trial version of Glyphs that I've played with, but I don't want to have to move to another font program to get this done.

Any help would be appreciated.

回答1:

In iOS 6 (and below) UIKit controls like UILabel, UITextView and UITextField use WebKit under the hood to render their contents. WebKit doesn't honor a font's built-in kerning. iOS 6 introduced attributed text support for all UIKit controls. If you set the attributedText property of a UILabel instead of the regular text property then CoreText rendering will kick in and you'll get proper kerning. However, in iOS 6 there's a bug with UITextField where font kerning will still be wrong even if setting attributedText.

If you really need proper kerning for labels in iOS 5 there's a lot of great drop-in replacements. I use TTTAttributedLabel but OHAttributedLabel is also great.

If you really need proper kerning for a UITextField you're probably going to want to use Omni's OUIEditableFrame. The downside is that's a pain to integrate into a project. I recently went through the learning process a wrote a lengthy tutorial if you're interested.