1

I'm not sure if I'm missing something obvious but I've looked around on SO and couldn't find an answer.

Basically, despite what this question says, UILabel plain text and attributed text looks different. For example, both labels below are system font 15pt, with the black one using attributed text and the gray one using plain text. Apparently the black one is wider than the gray one.

enter image description here

I understand I can set kerning on attributed text to adjust its appearance. My question is though what's the value I should set to make it look the same as Apple's default? Is there a way for me to "leave the kerning value at the default"?

Thank you!

danqing
  • 3,348
  • 2
  • 27
  • 43

1 Answers1

2

According to the documentation for the UILabel attributed text property

To turn on auto-kerning on the label, set kern of the string to null.

Looks like auto-kerning for plain text is on by default, for attributed text is off by default. Here's how to turn auto-kerning back on for the attributed text property:

        let label = UILabel()
        let attributes = [NSAttributedString.Key.kern: kCFNull!]
        label.attributedText = NSMutableAttributedString(string: "Text", attributes: attributes)
glotcha
  • 558
  • 6
  • 13
  • I've already tried this but it didn't seem to work. What I did was `string.addAttribute(.kern, value: kCFNull!, range: NSMakeRange(0, string.length))` – danqing Jul 08 '19 at 08:48
  • I added some sample code to the answer, it appears to set the kerning to "auto" as far as I can tell. I hope it helps. – glotcha Jul 08 '19 at 09:47
  • how did it go? do the text and attributed text match for you now? Nice to see someone sweat the typographical details ;-) – glotcha Jul 11 '19 at 01:15