如何修复 UILabel 文本间距?

use*_*242 5 uilabel ios uicollectionview uicollectionviewcell swift

此代码在 iOS 12 及更低版本上运行良好,并且在运行 iOS 13 时会出现问题。目标是将行高间距删除为 0,以便我的标签在文本之间减少空间量。我在集合视图单元格中有两个标签,当我将单元格从屏幕上滚动然后向下滚动时,标签文本现在被“切断”了。这不是我在以前版本的 iOS 中提到的情况。任何解决此问题的帮助都将是惊人的。提前致谢。

这是我的代码:

extension: UILabel {

        func addLineSpacing(spacing: CGFloat) {
        guard let text = text else { return }

        let originalText = NSMutableAttributedString(string: text)
        let style = NSMutableParagraphStyle()
        let lineHeight = font.pointSize - font.ascender + font.capHeight
        let offset = font.capHeight - font.ascender
        let range = NSRange(location: 0, length: text.count)

        style.maximumLineHeight = lineHeight
        style.minimumLineHeight = lineHeight
        style.alignment = .center

        originalText.addAttribute(.paragraphStyle, value: style, range: range)
        originalText.addAttribute(.baselineOffset, value: offset, range: range)

        attributedText = originalText
    }
}
Run Code Online (Sandbox Code Playgroud)

这是 UILabel 文本在滚动之前的样子:

在此处输入图片说明

这是滚动后的样子。请注意文本似乎是如何向上移动并被切断的

在此处输入图片说明

小智 4

我对 UILabel 也有类似的问题,我用以下方式修复了它:

style.maximumLineHeight = lineHeight
style.minimumLineHeight = lineHeight - 0.0001
Run Code Online (Sandbox Code Playgroud)

我知道这不是最漂亮的解决方案,这只是一个解决方法,但它确实有效。希望有帮助。