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

How to remove bottom padding of UILabel with attributedText inside UIStackView

我想删除 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

"bottom spacing" 不是 "spacing" ...您转换的 <p>...</p> html 块在文本末尾添加了一个换行符。

您可以使用此扩展程序(找到 ):

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 存储库):