danqing
danqing

Reputation: 3668

What's the default kerning for UILabel (attributed text looks different)

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!

Upvotes: 1

Views: 679

Answers (1)

glotcha
glotcha

Reputation: 578

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)

Upvotes: 2

Related Questions