UILabel wrong word wrap in iOS 11

13,048

Solution 1

This is a change by Apple to prevent widowed lines. From a design perspective, it is preferred to avoid having a single word on a line of text. UILabel now breaks the line in a way that the second line of text always has at least 2 words on it.

See the answer below for an option to disable it.

enter image description here

Also here's a good article about "widowed" and "orphaned" text.

Solution 2

Launching the app with the arguments -NSAllowsDefaultLineBreakStrategy NO (an undocumented defaults setting) seems to force back to the old behavior. Alternatively, you can set NSAllowsDefaultLineBreakStrategy to NO in NSUserDefaults at startup (Apple registers a default of YES for that value when UILabel or the string drawing code is initialized, it appears, so you would need to register an overriding value after that, or insert it into the NSArgumentDomain, or just set the default persistently).

Apple may consider that private API and reject apps that use it; I'm not sure. I have not tried this in a shipping app. However, it does work in quick testing -- saw the setting in NSUserDefaults and found changing it altered the behavior.

Solution 3

Since iOS 14 you can use lineBreakStrategy property of UILabel instance to control this behavior.

Available values are:

NSParagraphStyle.LineBreakStrategy() // none
NSParagraphStyle.LineBreakStrategy.pushOut
NSParagraphStyle.LineBreakStrategy.hangulWordPriority
NSParagraphStyle.LineBreakStrategy.standard

To disable this behavior using Swift:

if #available(iOS 14.0, *) {
    label.lineBreakStrategy = []
}

// Alternatives
// label.lineBreakStrategy = NSParagraphStyle.LineBreakStrategy()
// label.lineBreakStrategy = .init(rawValue: 0)
// label.lineBreakStrategy = .init()

To make it work on lower iOS versions, you can use NSAttributedString:

let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakStrategy = []

let attributedString = NSAttributedString(string: "Your text here", attributes: [
    .paragraphStyle: paragraphStyle
])

let label = UILabel()
label.attributedText = attributedString

Objective-C:

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

Solution 4

This is not really an answer, but I want to add an illustration of how it is a general problem, not at all related to ampersands.

two UILabels

Both of these UILabels have identical width constraints, and the text is almost identical. But the second has the word wrap I would expect. The first is incorrect, the "about" can clearly stay on the first line.

Solution 5

It seems that replacing the space before the ampersand with a non-breaking space (U+00A0) keeps the ampersand on the same line. Depending on how you are generating the text for the label, this might not be easy to automate (maybe you really do need the ampersand to be on the second line in some cases).

Share:
13,048
Admin
Author by

Admin

Updated on June 06, 2022

Comments

  • Admin
    Admin about 2 years

    I have problem with application using XIBs without autolayout. I don't know if this is important information.

    I have UILabel with 2 lines using word wrap. In iOS 10 word wrap was working correctly, and first line contained one word + special character, for example ampersand. Example:

    UiLabel on ios 10

    Then on iOS 11 word wrap is working somehow wrong and puts ampresand to the second line:

    UiLabel on ios 11

    This is problematic as longer words, that normally fitted on second line now are not being shown correctly. Any idea what has changed? I know about safeArea but it doesn't look like reason. Any ideas how to move that ampersand to the top where is plenty of space for it?

    Rest of the settings: size inspector