简体   繁体   English

UILabel换行在iOS 11上为时过早

[英]UILabel line breaks too early on iOS 11

I have an iOS 11 app with a table with cells. 我有一个带有单元格表格的iOS 11应用程序。

In this cells, I have a UILabel. 在此单元格中,我有一个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!). UILabel似乎“决定”以使底线至少包含2个单词的方式来断行(无论BreakLineMode是什么!)。

I know about using - Adding non-breaking space (U+00A0) - TOO EXPENSIVE 我知道使用-添加不间断空格(U + 00A0)-过于昂贵

and

Using NSAllowsDefaultLineBreakStrategy NO on NSUserDefaults - NOT ALLOWED BY APPLE 在NSUserDefaults上使用NSAllowsDefaultLineBreakStrategy NO-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. 这不是宽度问题,这是iOS 11 NEW UILabel逻辑(!)的问题,该逻辑不允许换行上的孤立单词(一个单词),只有两个单词,因为他们认为它看起来更好。 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. 但是必须有一种方法可以使UILabel在iOS 11上起作用,就像在iOS 10及更低版本上一样。

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

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. 在字符串的末尾添加很多空格(例如10-20)将使UILabel“认为”该行足够长,因此将在底行留下一个单词。 (instead of 2 so the first line will not break early) (而不是2,因此第一行不会提前中断)

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 我不喜欢黑客,但这是我目前所拥有的,如果有人有不同的答案,我会很乐意接受它而不是这个答案。

In my case I was able to solve this by using a textview. 就我而言,我能够通过使用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. UITextView允许孤儿,如果禁用了滚动,则根据内部大小来调整大小;如果禁用了交互,则其工作方式类似于label;在label中也禁用了该行为,因此它将转发所有触摸。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM