For what apparently is a 'bug,' UITextView does not seem to support kerning like other UIKit Elements. Is there a workaround to get kerning working?
To be clear, I'm talking about the spacing in between pairs of characters that are defined in the font file, not the general spacing between all characters. Using an NSAttributedString
with the NSKernAttributeName
will adjust the spacing between all characters, but the built in character pairs still don't work.
For Example:
Is there a workaround to fix this?
One simple workaround I have discovered is to add css styles to the UITextView which enable kerning. If I use the undocumented method setContentToHTMLString:
I can inject the css to the secret webview within the text view.
NSString *css = @"*{text-rendering: optimizeLegibility;}";
NSString *html = [NSString stringWithFormat:@"<html><head><style>%@</style></head><body>Your HTML Text</body></html>", css];
[textView performSelector:@selector(setContentToHTMLString:) withObject:html];
This fixes the problem immediately; however, it seems very likely this will get the app rejected. Is there a safe way to sneak some css into the text view?
Other workarounds I have experimented with include:
Using a webview and the contenteditable property. This isn't ideal because webview does a bunch of extra stuff that gets in the way, for example the input accessory view which can't easily be hidden.
Subclassing a UITextView
and rendering text manually with core text. This is more complex than I'd hoped because all the selections stuff needs to be reconfigured as well. Because of UITextView
's private subclasses of UITextPosition
and UITextRange
this is a huge pain in the ass if not completely impossible.