【问题标题】:Adding NSMutableParagraphStyle lineSpacing cause text to not fit UILabel添加 NSMutableParagraphStyle lineSpacing 导致文本不适合 UILabel
【发布时间】:2015-04-25 17:57:25
【问题描述】:

我创建了一个普通的 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)

谁能告诉我这是为什么以及我如何解决它?

【问题讨论】:

  • 嘿,我也遇到了同样的问题。你想通了吗?当我找到解决方案时,我会发布。

标签: swift uilabel nsparagraphstyle


【解决方案1】:

删除NSMutableParagraphStyle 即可。 我不知道为什么,但是这个属性是破坏文本字体大小调整的原因。

【讨论】:

    【解决方案2】:

    可能的解决方案是:

    1. 在storyboard\xib中设置paragraphStyle相关属性(lineHeightMultiplier、alignment等); (screenshot 1, screenshot 2)
    2. 在更改标签的属性文本之前获取 paragraphStyle
    3. 创建您需要的属性字符串;
    4. paragraphshStyle 属性添加到属性字符串;
    5. 将创建的 attributedString 设置为 attributedText 属性。

    如果不使用任何 syntax-sugar-lib,它可能看起来像这样:

        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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 2015-01-28
      相关资源
      最近更新 更多