How can I activate automatic hyphenation in iOS?
I have tried to set the hyphenation factor to 1 in the attributed text options of an UILabel, however I don't get any hyphens though.
How can I activate automatic hyphenation in iOS?
I have tried to set the hyphenation factor to 1 in the attributed text options of an UILabel, however I don't get any hyphens though.
UITextView
instead of anUILabel
. ThehyphenationFactor
(either as aNSParagraphStyle
attribute or as aNSLayoutManager
property) should work then (thanks to the new TextKit).UIWebView
and the-webkit-hyphens
CSS properties.CFStringGetHyphenationLocationBeforeIndex()
function that you mentioned in a comment. This function only gives you a hint about where to put hyphens in a string for a specific language. Then you have to break your lines of text yourself using the Core Text functions (likeCTLineCreateWithAttributedString()
and all). See Getting to Know TextKit (the paragraph called Hyphenation explains the logic of the Core Text process, with no code) and Hyphenation with Core Text on the iPad (gives some code sample, but the website seems to be down right now). It's probably going to be more work than you want!Swift version:
CoreText or TextKit
You need to add "soft hyphenation" to the string. These are "-" which is not visible when rendered, but instead merely queues for CoreText or UITextKit to know how to break up words.
The soft hyphen sign which you should place in the text is:
Example code
Outputs
ac-ces-si-bil-i-ty tests and frame-works check-ing
NSString+SoftHyphenation.h
NSString+SoftHyphenation.m