The default behavior for UILabel is that it prevents orphan words to appear solely on a separate line. ie: if word wrapping happen to keep 1 word alone at the last line. iOS will prevent that by sending a word from the line before it, having two words in the last line.
The problem is that this feature doesn’t work by default with NSMutableAttributedString
. how can I enable it?
Sample:
var string = customField?.title ?? ""
if customField?.required == true {
string += " *"
} else {
string += " ((getLocalizedString(localizedKey: .optional)))"
}
let style = NSMutableParagraphStyle()
if #available(iOS 14.0, *) {
style.lineBreakStrategy = .standard
}
let att = NSMutableAttributedString(string: string, attributes: [.paragraphStyle: style])
titleLabel.attributedText = att
Have in mind I am forced to use NSMutableAttributedString
for other reasons. 2 labels won’t work for me.
3
Answers
From the documentation of the
lineBreakStrategy
property onUILabel
, which helps control this behavior:If you want to use a specific line break strategy, like
.standard
("The text system uses the same configuration of line-break strategies that it uses for standard UI labels. "), you will need to apply the attribute to the attributed string via a paragraph style:Depending on your text, it may also help to set
allowsDefaultTighteningForTruncation
on the paragraph style because that may allow the text system to tighten the space between words on the last line of the string to get everything to fit. (I say may because this property controls truncation specifically, but it’s possible that the text system can take it into account for wrapping as well.)As per OP’s comments…
The issue is not with Attributed Text, as the same thing happens with "normal" text.
With iOS 11 (may have been 10), Apple changed UIKit to prevent orphans when a
UILabel
wraps to two lines of text. Orphans are still allowed with more than two lines:A
was prior to iOS 11…B
is current…C
is current with more than two lines…Note the
D
example — I don’t have the Xcode beta installed, but based on other comments I’ve seen it appears that in iOS 16 the "no orphan" rule will also be applied when the text wraps to more than two lines.So… a way to solve your issue is to use a "non-break-space" character between the last word and the asterisk (instead of a plain space).
Here’s a quick test:
Output:
So… you’ll want to test that with iOS 16, and, if that’s the case, you may need to do a version check to determine wether to add a plain space or a non-break-space.
It is a change by Apple since iOS11 (as answered by @DonMag) to prevent orphaned word in the last line.
If your production only support iOS13.0+, setting the lineBreakStrategy will set it back to the old style.
(One interesting thing is, I found this lineBreakStrategy also work on iOS 13.0+, even tho from Apple’s document it mentioned iOS 14.0+.)
If you need to support older iOS version, you need to set the value of the
NSAllowsDefaultLineBreakStrategy
key when application launch, which I cannot find any document about it. I tested it worked on iOS 11 & 12, but not on iOS 13.0+.So you might need both if your app support iOS 11.0+. Hope it helps 😉