让 UILabel 生成省略号而不是缩小字体

发布于 2024-12-09 00:10:26 字数 243 浏览 2 评论 0原文

当我动态更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

孤凫 2024-12-16 00:10:26

设置以下属性:

Objective C

label.adjustsFontSizeToFitWidth = NO;
label.lineBreakMode = NSLineBreakByTruncatingTail;

Swift

label.adjustsFontSizeToFitWidth = false
label.lineBreakMode = .byTruncatingTail

您还可以在界面生成器中设置这些属性。

Set the following properties:

Objective C

label.adjustsFontSizeToFitWidth = NO;
label.lineBreakMode = NSLineBreakByTruncatingTail;

Swift

label.adjustsFontSizeToFitWidth = false
label.lineBreakMode = .byTruncatingTail

You can also set these properties in interface builder.

惟欲睡 2024-12-16 00:10:26

在设置 UILabel 样式后,我遇到了生成省略号的问题,需要使用 UILabel.attributedText 而不是 UILabel.text。段落样式中有一个换行模式,在使用属性文本时会覆盖 UILabel.lineBreakMode。如果要实现省略号,您需要将属性字符串的段落样式的 lineBreakMode 设置为 .byTruncatingTail

例如

    let text = "example long string that should be truncated"
    let attributedText = NSMutableAttributedString(
        string: text, 
        attributes: [.backgroundColor : UIColor.blue.cgColor]
    )
    let range = NSRange(location: 0, length: attributedText.length)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .byTruncatingTail
    attributedText.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
    uiLabel.attributedText = attributedText

I had an issue producing ellipsis after I styled a UILabel and needed to use UILabel.attributedText instead of UILabel.text. There is a line break mode on the paragraph style that will overwrite the UILabel.lineBreakMode when using attributed text. You'll need to set the lineBreakMode to .byTruncatingTail on the attributed string's paragraph style if you want to achieve ellipsis.

e.g.

    let text = "example long string that should be truncated"
    let attributedText = NSMutableAttributedString(
        string: text, 
        attributes: [.backgroundColor : UIColor.blue.cgColor]
    )
    let range = NSRange(location: 0, length: attributedText.length)
    let paragraphStyle = NSMutableParagraphStyle()
    paragraphStyle.lineBreakMode = .byTruncatingTail
    attributedText.addAttribute(.paragraphStyle, value: paragraphStyle, range: range)
    uiLabel.attributedText = attributedText
梦太阳 2024-12-16 00:10:26

斯威夫特解决方案:

label.lineBreakMode = .ByTruncatingTail

斯威夫特 3:

label.lineBreakMode = .byTruncatingTail

Swift solution:

label.lineBreakMode = .ByTruncatingTail

Swift 3:

label.lineBreakMode = .byTruncatingTail
鲸落 2024-12-16 00:10:26

在界面生成器中,这是要走的路:

Interface Builder 中的换行符设置

In the interface builder this is the way to go:

Line Break setting in Interface Builder

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文