UILabel line breaks too early on iOS 11

时光毁灭记忆、已成空白 提交于 2019-12-11 02:01:48

问题


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

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.


回答1:


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



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/49751185/uilabel-line-breaks-too-early-on-ios-11

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!