【问题标题】:How to fix UILabel text spacing?如何修复 UILabel 文本间距?
【发布时间】:2019-09-23 19:51:39
【问题描述】:

此代码在 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
    }
}

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

这是滚动后的样子。注意文本似乎是如何向上移动和截断的

【问题讨论】:

  • 可能需要查看单元格的配置方式以及调用此方法的时间/地点。

标签: ios swift uicollectionview uilabel uicollectionviewcell


【解决方案1】:

我在使用 UILabel 时遇到了类似的问题,我通过以下方式修复了它:

style.maximumLineHeight = lineHeight
style.minimumLineHeight = lineHeight - 0.0001

我知道这不是最完美的解决方案,这只是一种解决方法,但它确实有效。希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2011-03-29
    • 2022-06-27
    • 2015-11-24
    • 2020-12-01
    • 2016-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多