[英]How to make UILabel text appear with ellipsis at the end when text is longer than label's width
My issue is with a UILabel
.我的问题是
UILabel
。
The text it holds is much longer than the actual width of the UILabel.它保存的文本比 UILabel 的实际宽度长得多。 So I would like it to appear with "..." (an ellipsis) at the end to denote that there's more text.
所以我希望它在末尾带有“...”(省略号)以表示还有更多文本。
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.现在它只是在宽度填满时切割文本。
It is fairly easy:这很容易:
Create a UILabel called "aLabel" for example.例如,创建一个名为“aLabel”的 UILabel。
Create an IBOutlet.创建一个IBOutlet。
Do:做:
aLabel.adjustsFontSizeToFitWidth = false aLabel.lineBreakMode = .byTruncatingTail
If you are adding UIlabel through a Storyboard, following steps can be useful:如果您通过 Storyboard 添加 UIlabel,以下步骤可能很有用:
Select the label which you want to display '...' at the end.选择要在末尾显示“...”的标签。
Go to attributes inspector .转到属性检查器。
Select ' Line Breaks ' and choose an option Truncate Tail选择“换行符”并选择一个选项Truncate Tail
Check it by giving a text of more content size than label size.通过提供比标签大小更大的内容大小的文本来检查它。
Objective C:目标 C:
yourUILabel.adjustsFontSizeToFitWidth = false;
yourUILabel.lineBreakMode = NSLineBreakByTruncatingTail;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.