【问题标题】:How to remove bottom padding of UILabel with attributedText inside UIStackView如何使用 UIStackView 中的属性文本删除 UILabel 的底部填充
【发布时间】:2020-08-31 17:45:03
【问题描述】:

我想在 UIStackview 中使用 attributedText 删除 UILabel 的底部填充。

我找到了这个解决方案How to remove the extra padding below an one line UILabel。这适用于普通文本,但不适用于属性文本。

   let textLabel = UILabel()
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        textLabel.text = "What is a chemical property and how can you observe it?"
        textLabel.numberOfLines = 0
        textLabel.lineBreakMode = .byWordWrapping
        textLabel.backgroundColor = .lightGray
        mainStackView.addArrangedSubview(textLabel)

        let textLabel2 = UILabel()
        textLabel2.translatesAutoresizingMaskIntoConstraints = false

        let html = "<html lang=\"en\"><head><meta charset=\"UTF-8\"></head><body><div style=\"font-size:36;\"><p>What is a <em>chemical property</em> and how can you observe it?</p></div></body></html>"

        let data = Data(html.utf8)

        if let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil) {

            let a = NSMutableAttributedString.init(attributedString: attributedString)

            let range = (a.string as NSString).range(of: a.string)

            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.alignment = .left
            paragraphStyle.firstLineHeadIndent = 0.0

            let attributes: [NSAttributedString.Key: Any] = [
                .foregroundColor: UIColor.black,
                .paragraphStyle: paragraphStyle
            ]

            a.addAttributes(attributes, range: range)
            textLabel2.attributedText = a
        }
        textLabel2.numberOfLines = 0
        textLabel2.lineBreakMode = .byWordWrapping
        textLabel2.backgroundColor = .yellow

        mainStackView.addArrangedSubview(textLabel2)

        let textLabel3 = UILabel()
        textLabel3.translatesAutoresizingMaskIntoConstraints = false
        textLabel3.text = "What is a chemical property and how can you observe it?"
        textLabel3.numberOfLines = 0
        textLabel3.lineBreakMode = .byWordWrapping
        textLabel3.backgroundColor = .lightGray
        mainStackView.addArrangedSubview(textLabel3)

可以在此处找到包含此代码的工作示例项目:https://github.com/Quobject/testUIlabelInStackviewpadding

【问题讨论】:

    标签: ios swift3 uilabel nsattributedstring uistackview


    【解决方案1】:

    “底部间距”不是“间距”...您转换后的 &lt;p&gt;...&lt;/p&gt; html 块在文本末尾添加了一个换行符。

    你可以使用这个扩展(找到here):

    extension NSMutableAttributedString {
    
        func trimmedAttributedString() -> NSAttributedString {
            let invertedSet = CharacterSet.whitespacesAndNewlines.inverted
            let startRange = string.rangeOfCharacter(from: invertedSet)
            let endRange = string.rangeOfCharacter(from: invertedSet, options: .backwards)
            guard let startLocation = startRange?.upperBound, let endLocation = endRange?.lowerBound else {
                return NSAttributedString(string: string)
            }
            let location = string.distance(from: string.startIndex, to: startLocation) - 1
            let length = string.distance(from: startLocation, to: endLocation) + 2
            let range = NSRange(location: location, length: length)
            return attributedSubstring(from: range)
        }
    }
    

    并改变这一行:

    textLabel2.attributedText = a
    

    到:

    textLabel2.attributedText = a.trimmedAttributedString()
    

    结果(将更改应用到您的 GitHub 存储库):

    【讨论】:

    • 很棒的东西!
    猜你喜欢
    • 2016-01-24
    • 2018-11-23
    • 2018-02-04
    • 2015-04-21
    • 2011-04-07
    • 1970-01-01
    • 2012-09-02
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多