让 UILabel 生成省略号而不是缩小字体
当我动态更改 UILabel 的文本时,我更喜欢使用省略号(点、点、点),而不是自动调整文本大小。如何做到这一点?
换句话说,如果我的 UILabel 包含单词 Cat
,字体大小为 14,然后我将该单词更改为 Hippopotamus
,则字体会缩小以适合所有单词。我宁愿这个词被自动截断,后跟省略号。
我假设我的 UILabel 对象中有一个可以更改的参数。我宁愿不以编程方式执行此操作。
When I dynamically change the text of a UILabel I would prefer to get an ellipsis (dot, dot, dot) rather then have the text be automatically resized. How does one do this?
In other words, if I have UILabel with the word Cat
with size font 14 and then I change the word to Hippopotamus
the font shrinks to fit all the word. I would rather the word be automatically truncated followed by an ellipsis.
I assume there is a parameter that can be changed within my UILabel object. I'd rather not do this programmatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
设置以下属性:
Objective C
Swift
您还可以在界面生成器中设置这些属性。
Set the following properties:
Objective C
Swift
You can also set these properties in interface builder.
在设置 UILabel 样式后,我遇到了生成省略号的问题,需要使用 UILabel.attributedText 而不是 UILabel.text。段落样式中有一个换行模式,在使用属性文本时会覆盖
UILabel.lineBreakMode
。如果要实现省略号,您需要将属性字符串的段落样式的lineBreakMode
设置为.byTruncatingTail
。例如
I had an issue producing ellipsis after I styled a UILabel and needed to use
UILabel.attributedText
instead ofUILabel.text
. There is a line break mode on the paragraph style that will overwrite theUILabel.lineBreakMode
when using attributed text. You'll need to set thelineBreakMode
to.byTruncatingTail
on the attributed string's paragraph style if you want to achieve ellipsis.e.g.
斯威夫特解决方案:
斯威夫特 3:
Swift solution:
Swift 3:
在界面生成器中,这是要走的路:
In the interface builder this is the way to go: