gpt4 book ai didi

ios - UILabel 文本截断与文本换行

转载 作者:行者123 更新时间:2023-11-28 06:42:57 31 4
gpt4 key购买 nike

我有一个 UILabel 作为 titleView 放在导航栏中。我希望它有 2 行,其中第一行可以被截断,第二行居中对齐。

在代码中它看起来更不像这样:

    let label = UILabel()
let text = NSAttributedString(string: "Long long long text\nsecond line")
label.attributedText = text
label.textAlignment = .Center
label.numberOfLines = 0
label.lineBreakMode = .ByTruncatingTail
label.sizeToFit()

self.navigationItem.titleView = label

The effect in case of the first line text is not exceeding available space is like this:

enter image description here

It's pretty good, but when the first line text is longer than:

let text = NSAttributedString(string: "Very very very very very long text\nsecond line")

enter image description here

I want to achieve like below.

enter image description here

如何实现?我尝试了 numberOfLines 和 lineBreakMode,但没有用。

最佳答案

将换行模式更改为 ByTruncatingMiddle 而不是 ByTruncatingTail。像下面这样的东西,

    label.lineBreakMode = .ByTruncatingMiddle

希望这会有所帮助:)

关于ios - UILabel 文本截断与文本换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37409260/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com