如何在UIStackView内使用attributedText删除UILabel的底部填充

问题描述 投票:0回答:1

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

enter image description here

我找到了此解决方案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个回答
2
投票

“底部间距”不是“间距” ...您转换的<p>...</p> 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存储库):

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.