studentbi
studentbi

Reputation: 825

UILabel wrong word wrap in iOS 11

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

Upvotes: 69

Views: 21174

Answers (8)

Karlis
Karlis

Reputation: 1910

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;
}

Upvotes: 36

brynbodayle
brynbodayle

Reputation: 6626

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.

Upvotes: 90

Jonathan.
Jonathan.

Reputation: 55594

A bit of a hack but you can add some zero width spaces to the end of the string to restore the old behaviour, without affecting the layout of the string otherwise that you'd get from normal spaces:

let zeroWidthSpace: Character = "\u{200B}"
let spacingForWordWrapping = String(repeating: zeroWidthSpace, count: 6)
label.text = "oneText & two" + spacingForWordWrapping

Upvotes: 4

Patrick
Patrick

Reputation: 107

As a simple (hacky) workaround, you can often get the correct behaviour with a UILabel by adding spaces at the end of your text. Using your example:

Wraps the new (undesired) way:
"oneText & two."

Wraps the old way:
"oneText & two. " (note the 2 extra spaces at the end of the string)

The obvious downside is if those extra spaces get forced to a new line by themselves, but for something simple like a title it's often enough.

Upvotes: -1

Carl Lindberg
Carl Lindberg

Reputation: 2972

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.

Upvotes: 24

Carl Lindberg
Carl Lindberg

Reputation: 2972

An option may be to use a UITextView instead -- that does not seem to have this behavior. If you set the NSTextContainer.lineFragmentPadding to 0, the textContainerInset to UIEdgeInsetsZero, and turn off all scrolling (scrollEnabled, bounces, scroll indicators, etc.) it will display similarly to a UILabel, though not with as much constraint flexibility. It's not a drop-in replacement, but in some situations it's acceptable.

Upvotes: 1

David Dunham
David Dunham

Reputation: 8329

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.

Upvotes: 14

Geoff Hackworth
Geoff Hackworth

Reputation: 3094

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).

Upvotes: 1

Related Questions