9

I thought I would save others hours of debugging and say that setting adjustsFontForContentSizeCategory to true has no affect on labels you set the attributedText value of (at least in tableview cell labels).

Luckily the solution is to set the font on the attributed string yourself. I wrote a small utility extension:

public extension NSMutableAttributedString {
    public func setFont(_ font: UIFont) -> NSMutableAttributedString {
        addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: string.count))

        return self
    }
}

Where you set the attributedText property you would call

@IBOutlet weak var myLabel: UILabel!

var myAttributedString: NSAttributedString = .....

myLabel.attributedText = NSMutableAttributedString(attributedString: myAttributedString).setFont(myLabel.font)
GilroyKilroy
  • 847
  • 10
  • 17

1 Answers1

0
public extension NSMutableAttributedString {
public func setFont(_ font: UIFont) -> NSMutableAttributedString {
    addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: string.count))

    return self
}

}

GilroyKilroy
  • 847
  • 10
  • 17