fedoroffn
fedoroffn

Reputation: 427

Font attribute (NSFontAttributeName) not applied to attributed string

I'm trying to apply two attributes to a string, one for kerning (NSKernAttributeName) and one for the font (NSFontAttributeName). Although I've checked and rechecked 1000 times, I can only get the kerning attribute to be applied to the string. Here's my setup:

    let runAtTitle = "RUN AT"
    var attRunAt = NSMutableAttributedString(string: runAtTitle)

    let font = UIFont(name: "HelveticaNeue-Bold", size: 76.0)!
    let attrs = [NSFontAttributeName: font, NSKernAttributeName: -3.0]
    attRunAt.addAttributes(attrs, range: NSMakeRange(0, attRunAt.length))

    runAtLabel.attributedText = attRunAt

When I build the app, the kerning is applied to my string, but the font is not. It uses the default 12pt Helvetica. Please tell me I'm doing something wrong.

Upvotes: 0

Views: 2141

Answers (1)

Mundi
Mundi

Reputation: 80271

I just tried your code in Playground and it works fine. Most likely you are setting the text attribute of the label after setting the attributedText. That will revert the string to the normal label string with its attributes, or perhaps for some strange reason keep the kern attribute.

Here is what I get with your code:

and after adding

label.text = "RUN AT"

Upvotes: 1

Related Questions