当文本包含线路断开和标签的台词和LINEBREACKMODE时,Uilabel在最后一行中显示不正确的文本。ByTruncatingTail

发布于 2025-01-24 06:51:33 字数 1742 浏览 0 评论 0原文

如果在下面运行代码段,它将在最后一行中显示不正确的文本,该文本具有...省略号,如该图像所示:

“在此处输入映像说明”

一旦将linebreakmode更改为bywordwrapprapped < /code>,或将numberOflines设置为0,然后正确显示文本。另外,如果您从文本中删除demake /n,则文本将正确显示。

因此,我的问题是:如何在Uilabel的文本中使用A /N线路断开,并使用有限的数字线和LineBreakMode .BytruncatingTail而又不会在最后一行显示不正确的文本?

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let text = "A line break challenge \nInvestigating a bug where the text in the last line is incorrect when numberOfLines is limited to for example 3. Very strange indeed. Peculiar. What to do about this? What am I missing?"
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .byTruncatingTail //.byWordWrapping works, but not what I want.

        let attributedString = NSAttributedString(string: text, attributes: [
            .paragraphStyle: paragraphStyle
        ])
        
        let label = UILabel()
        label.attributedText = attributedString
        label.numberOfLines = 3 //0 works, but is not what I want.

        label.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(label)
        
        label.topAnchor.constraint(equalTo: view.topAnchor, constant: 40.0).isActive = true
        label.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20.0).isActive = true
        label.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20.0).isActive = true
    }
}

If you run the code snippet below, it will show incorrect text in the last line that has the ... ellipsis, as show in this image:

enter image description here

Once you change the lineBreakMode to byWordWrapping, or set the numberOfLines to 0, then the text is displayed correctly. Also, if you remove the line break /n from the text, then the text is displayed correctly.

So my question is: How can I use a /n line break in the text in a UILabel with a limited numberOfLines and lineBreakMode .byTruncatingTail without showing incorrect text on the last line?

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let text = "A line break challenge \nInvestigating a bug where the text in the last line is incorrect when numberOfLines is limited to for example 3. Very strange indeed. Peculiar. What to do about this? What am I missing?"
        
        let paragraphStyle = NSMutableParagraphStyle()
        paragraphStyle.lineBreakMode = .byTruncatingTail //.byWordWrapping works, but not what I want.

        let attributedString = NSAttributedString(string: text, attributes: [
            .paragraphStyle: paragraphStyle
        ])
        
        let label = UILabel()
        label.attributedText = attributedString
        label.numberOfLines = 3 //0 works, but is not what I want.

        label.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(label)
        
        label.topAnchor.constraint(equalTo: view.topAnchor, constant: 40.0).isActive = true
        label.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 20.0).isActive = true
        label.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -20.0).isActive = true
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文