热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

UILabel在ios11中出现了错误的词包-UILabelwrongwordwrapiniOS11

IhaveproblemwithapplicationusingXIBswithoutautolayout.Idontknowifthisisimportantinf

I have problem with application using XIBs without autolayout. I don't know if this is important information.

我对使用XIBs而不使用autolayout有问题。我不知道这是不是重要的信息。

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和2行使用word wrap。在ios10中,换行是正确的,第一行包含一个单词+特殊字符,例如&符号。例子:

UiLabel on ios 10

Then on iOS 11 word wrap is working somehow wrong and puts ampresand to the second line:

然后在ios11中,换行有点问题,把ampresand放到了第二行:

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

其他设置:

5 个解决方案

#1


26  

This could very likely be an intentional 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. So it seems that UILabel now breaks the line in a way that a the second line of text always has at least 2 words on it.

这很可能是苹果公司为防止丧偶生产线而故意做出的改变。从设计的角度来看,最好避免在一行文本中出现一个单词。看来UILabel现在打破了这条线,因为第二行总是至少有两个词。

It is surprising that there is no documentation or way to disable this behavior though.

令人惊讶的是,没有文档或方法禁用这种行为。

enter image description here

Also here's a good article about "widowed"and "orphaned text.

这里还有一篇关于“寡妇”和“孤儿文本”的好文章。

#2


7  

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.

这并不是一个真正的答案,但我想要说明的是它是一个普遍的问题,而不是与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.

这两个UILabels都有相同的宽度限制,而且文本几乎相同。但是第二个词是“wrap”这个词。第一个是不正确的,“about”可以明显地停留在第一行。

#3


3  

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

使用参数- nsallowsdefaultbreakstrategy NO(一个未文档化的默认设置)启动应用程序,似乎会迫使您回到旧的行为。另外,你可以设置在NSUserDefaults NSAllowsDefaultLineBreakStrategy没有启动(苹果注册是的的一个默认值UILabel或初始化字符串绘图代码时,它出现了,你需要注册一个压倒一切的价值之后,或将其插入到NSArgumentDomain,或者只是设置默认持续)。

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.

苹果可能会考虑这个私有API,拒绝使用它的应用;我不确定。我还没有在一个航运应用程序中尝试过这一点。但是,它确实可以在快速测试中使用——查看了NSUserDefaults中的设置,发现改变会改变它的行为。

#4


1  

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

似乎用一个不间断的空格(U+00A0)替换了与号之前的空格,使与号保持在同一条线上。根据如何为标签生成文本,这可能不容易自动化(在某些情况下,可能确实需要在第二行中使用&号)。

#5


0  

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.

一个选项可能是使用UITextView,而不是这种行为。如果您设置了NSTextContainer。行分割填充为0,textContainerInset设置为uiedgeinsets0,并关闭所有滚动(启用scrollEnabled、bounces、滚动指示器等),它将显示与UILabel类似的内容,但没有那么大的约束灵活性。这并不是替代,但在某些情况下是可以接受的。


推荐阅读
author-avatar
--AppleChan--
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有