简体   繁体   English

当文本长于标签宽度时,如何使 UILabel 文本在末尾显示省略号

[英]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:这很容易:

  1. Create a UILabel called "aLabel" for example.例如,创建一个名为“aLabel”的 UILabel。

  2. Create an IBOutlet.创建一个IBOutlet。

  3. Do:做:

     aLabel.adjustsFontSizeToFitWidth = false aLabel.lineBreakMode = .byTruncatingTail

If you are adding UIlabel through a Storyboard, following steps can be useful:如果您通过 Storyboard 添加 UIlabel,以下步骤可能很有用:

  1. Select the label which you want to display '...' at the end.选择要在末尾显示“...”的标签

  2. Go to attributes inspector .转到属性检查器

  3. Select ' Line Breaks ' and choose an option Truncate Tail选择“换行符”并选择一个选项Truncate Tail

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

相关问题 基于标签文本的宽度和高度时如何在UILabel后面发送UIVisualEffectView - How to send UIVisualEffectView behind UILabel when based on the label's text's width and height 当字体大小大于标签高度时,防止UILabel剪切文本 - Prevent UILabel from clipping it's text when the font size is larger than the label height 根据标签宽度更改 UILabel 的文本 - Change text of UILabel depending on label width 当一个文本比另一个文本长时 UILabel 文本更改动画闪烁 - UILabel text change animation flickers when one text is longer than other 文本较长时标签没有增长 - label is not growing when the text is longer 使 UILabel 的文本加粗 - make UILabel's text bold ios SWIFT-如何在文本长于字段宽度时在UITextField上启用水平滚动并禁用截断“…” - ios SWIFT - how to enable horizontal scrolling on UITextField and disable truncate “…” when text is longer than field width 设置为自动换行时如何获取UILabel文本的实际宽度 - How to get actual width of UILabel text when set to word wrap 设置UILabel文本花费的时间比预期的iOS7长 - Setting UILabel text taking longer than expected iOS7 调整文本大小以适合大小时,如何删除UILabel末尾的“…”? - How do I remove the “…” at end of UILabel when resizing text to fit?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM