添加NSMutableParagraphStyle的行间距导致文本无法适应UILabel

8

我创建了一个普通的UILabel,并希望能够为进入UILabel的文本添加行间距。

但是,当我这样做时,它会影响到adjustsFontSizeToFitWidth,不再适配于UILabel。

我使用过以下代码:

        var userQuestionsAnswer = UILabel(frame: CGRectMake(0, 0, 20, 20))
        userQuestionsAnswer.font = UIFont(name: Font.GothamBlack, size: 15)
        userQuestionsAnswer.numberOfLines = 0
        userQuestionsAnswer.adjustsFontSizeToFitWidth = true

        var style = NSMutableParagraphStyle()
        style.lineSpacing = view.frame.size.height * 0.021
        style.alignment = NSTextAlignment.Center
        let attributes = [NSParagraphStyleAttributeName : style]
        var answerText = "This is the answer"

        self.userQuestionsAnswer.attributedText = NSAttributedString(string: answerText!, attributes:attributes)

有人能告诉我原因并且如何解决吗?

嘿,我也遇到了同样的问题。你解决了吗?我找到解决方案后会发布的。 - justColbs
2个回答

0
删除NSMutableParagraphStyle,它就能正常工作了。我不知道为什么,但这个属性会导致文本字体大小调整出现问题。

-1
可能的解决方案是:
  1. 在storyboard\xib中设置paragraphStyle相关属性(lineHeightMultiplier、alignment等)(screenshot 1screenshot 2
  2. 在更改标签的属性文本之前获取paragraphStyle
  3. 创建所需的attributedString;
  4. paragrapshStyle属性添加到attributedString中;
  5. 将创建的attributedString设置为attributedText属性。

如果不使用任何语法糖库,它可能看起来像这样:

    func paragraphStyle(in attrString: NSAttributedString?) -> NSParagraphStyle? {
        return attrString?.attribute(NSParagraphStyleAttributeName, at: 0, effectiveRange: nil) as? NSParagraphStyle
    }

    let text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."

    let attributedText = NSMutableAttributedString(string: text, attributes: [
        NSFontAttributeName: UIFont.systemFont(ofSize: 20.0),
        NSForegroundColorAttributeName: UIColor.orange
    ])

    if let paragraphStyle = paragraphStyle(in: label.attributedText) {
        let textRange = NSMakeRange(0, text.characters.count)
        attributedText.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: textRange)
    }

    label.attributedText = attributedText

使用 pod 'SwiftyAttributes' 和 NSMutableAttributedString 扩展:

import SwiftyAttributes

extension NSMutableAttributedString {
    func withParagraphStyle(from attrString: NSAttributedString?) -> NSMutableAttributedString {
        guard let attrString = attrString, let pStyle = attrString.attribute(.paragraphStyle, at: 0) else {
            return self
        }

        return self.withAttribute(pStyle)
    }
}

代码将是:

    let text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book."

    label.attributedText = text.withAttributes([
            .font(.systemFont(ofSize: 20.0)),
            .textColor(.orange)
        ]).withParagraphStyle(from: label.attributedText)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接