xcode 当文本长于标签的宽度时,如何使 UILabel 文本以“...”结尾
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37091096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make UILabel text appear with "..." at the end when text is longer than label's width
提问by Polis
My issue is with a UILabel
.
我的问题是一个UILabel
.
The text it holds is much longer than the actual width of the UILabel. So I would like it to appear with "..." at the end to denote that there's more text.
它保存的文本比 UILabel 的实际宽度长得多。所以我希望它以“...”结尾,表示还有更多的文字。
I played around with horizontal content hugging priority (made it less than 251) but it doesn't seem to make a difference.
我玩过水平内容拥抱优先级(使其小于 251),但似乎没有什么区别。
Right now it just chops the text when the width fills up.
现在它只是在宽度填满时切割文本。
Any ideas?
有任何想法吗?
回答by Coder1000
It is fairly easy !
这很容易!
1) Create a UILabel called "aLabel" for example.
1) 例如创建一个名为“aLabel”的 UILabel。
2) Create an IBOutlet.
2)创建一个IBOutlet。
3) Do:
3)做:
aLabel.adjustsFontSizeToFitWidth = false
aLabel.lineBreakMode = .ByTruncatingTail
回答by Polis
If you are adding UIlabel through a Storyboard, following steps can be useful:
如果您通过 Storyboard 添加 UIlabel,以下步骤可能会很有用:
1) Select the labelwhich you want to display '...' at the end.
1) 选择要在末尾显示“...”的标签。
2) Go to attributes inspector.
2)转到属性检查器。
3) Select 'Line Breaks' and choose an option Truncate Tail
3)选择“换行符”并选择一个选项截尾
4) Check it by giving a text of more content size than label size.
4)通过提供比标签大小更大的内容大小的文本来检查它。
回答by Qianren Zhou
Objective C:
目标 C:
yourUILabel.adjustsFontSizeToFitWidth = false;
yourUILabel.lineBreakMode = NSLineBreakByTruncatingTail;