ozd
ozd

Reputation: 1284

UILabel line breaks too early on iOS 11

I have an iOS 11 app with a table with cells.

In this cells, I have a UILabel.

The UILabel seems to "decide" to break the line in a way that keeps the bottom line with at least 2 words (no matter what the BreakLineMode is!).

I know about using - Adding non-breaking space (U+00A0) - TOO EXPENSIVE

and

Using NSAllowsDefaultLineBreakStrategy NO on NSUserDefaults - NOT ALLOWED BY APPLE

What can I do to fix this problem?

In this image, you can see that the first word in the second line, could appear on the first line but got down to the second for some reason...

enter image description here

EDIT:

This is NOT a width issue, this is an issue with iOS 11 NEW UILabel logic(!) that doesn't allow orphans words (one word) on a new line, Only two word because they think it looks better. It doesn't. But there must be a way to make a UILabel act on iOS 11 like it did on iOS 10 and under.

Upvotes: 2

Views: 1862

Answers (3)

Amber K
Amber K

Reputation: 698

In my case I was able to solve this by using a textview. UITextView allows orphans, sizes according to intrinsic size if scrolling is disabled and works like label if interaction is disabled, which is also disabled in label so it will forward all the touches.

Upvotes: 0

ozd
ozd

Reputation: 1284

I have found an hack -

Adding a lot of spaces (like 10 - 20) to the end of the string will make the UILabel "think" that the line is long enough so it will leave one word on the bottom line. (instead of 2 so the first line will not break early)

I don't like hacks but this is all I've got at the moment, if anybody has a different answer i will gladly accept it instead of this one

Upvotes: 1

Rashed
Rashed

Reputation: 2425

You can add this three line of code -

labelName.adjustsFontSizeToFitWidth = true
labelName.minimumScaleFactor = 0.3

labelName.numberOfLines = 0 // or you can set it 1 or 2

Upvotes: 0

Related Questions