Adding NSMutableParagraphStyle lineSpacing causes the text to not match UILabel

I created a regular UILabel and want to be able to add line spacing to the text that is made in UILabel.

Although, when I do this, it affects adjustsFontSizeToFitWidth and is no longer set to UILabel.

Some code that I used:

        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)

Can someone tell me why this is and how I get around it?

+4
source share
2 answers

Remove NSMutableParagraphStyleand it will work. I do not know why, but this attribute is the reason for the violation of the font size adjustment of the text.

+1
source

Possible Solution:

  • paragraphStyle (lineHeightMultiplier, ..) \xib; ( 1, 2)
  • paragraphStyle ;
  • strring, ;
  • paragrapshStyle String;
  • set attribitedString attribitedText.

--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 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 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)
+1

Source: https://habr.com/ru/post/1584656/


All Articles