8

I have created a normal UILabel and want to be able to add line spacing to the text which does into the UILabel.

Although when I do this it affects the adjustsFontSizeToFitWidth and it is no longer fitted to the UILabel.

Some code I have 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 anyone tell my why this is and how I get around it?

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Henry Brown
  • 2,219
  • 8
  • 31
  • 48

2 Answers2

0

Remove the NSMutableParagraphStyle and it will work. I don't know why but this attribute is the cause of breaking text font size adjustment.

TomSawyer
  • 3,711
  • 6
  • 44
  • 79
-1

The possible solution is:

  1. set up paragraphStyle related attributes (lineHeightMultiplier, alignment etc.) in storyboard\xib; (screenshot 1, screenshot 2)
  2. get paragraphStyle before changing the attributed text of a label;
  3. create attributedString you need;
  4. add paragrapshStyle attribute to attributedString;
  5. set created attributedString as attributedText property.

Without using any syntax-sugar-lib it may look like this:

    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

Using pod 'SwiftyAttributes' and NSMutableAttributedString extension:

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)
    }
}

code will be:

    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)