Allowing Orphaned Words in iOS

In iOS 11 Apple changed the word wrapping behavior of UILabel by preventing a single word being orphaned, in order to make longer paragraphs of text more readable. In general it was a good move by Apple since it's a standard practice in typography, but it looks kind of weird with smaller chunks of text.

Before iOS11 -

After iOS11-

Up until iOS 14 our options to disable this practice were to call sizeToFit after the text was set, using CATextLayer instead, or even setting an undocumented app launch argumentNSAllowsDefaultLineBreakStrategy to NO.

Solution

Thankfully since iOS 14 we can adjust the line break behavior directly on UILabel instance using property lineBreakStrategy.

Available values are:

NSParagraphStyle.LineBreakStrategy.pushOut
NSParagraphStyle.LineBreakStrategy.hangulWordPriority
NSParagraphStyle.LineBreakStrategy.standard

None of them will fix disable the default behaviour though. To enable having a single word on a line of text we need to set the line break strategy to "none" like this:

if #available(iOS 14.0, *) {
label.lineBreakStrategy = .init()
}
// Alternatives
// label.lineBreakStrategy = NSParagraphStyle.LineBreakStrategy()
// label.lineBreakStrategy = .init(rawValue: 0)

And also in Objective-C:

if (@available(iOS 14.0, *)) {
label.lineBreakStrategy = NSLineBreakStrategyNone;
}

More on This Topic

Documentation