Question

I have the following code:

[[cancelButton titleLabel] setFont:[UIFont fontWithName:@"ProximaNova-Regular" size:15]];

How would I set the letter-spacing as well?

Was it helpful?

Solution

You can't change the letter spacing in the abstract, which means that you can't change it at all under iOS 5 and below.

As of iOS 6, you can push an attributed string rather than a vanilla one to a UILabel. The process for pushing an attributed string works slightly differently from the process for pushing an ordinary one — the font, text colour and a bunch of other properties are all set on the string rather than on the label. The reason is that attributed strings allow different attributes to be set for different regions of the string. So you can set a string that combines multiple fonts, text colours, etc.

One of the supported Core Text attributes is kCTKernAttributeName, which as of iOS 6 is easier to take advantage of via the UIKit addition NSKernAttributeName. You can use kerning to adjust the horizontal spacing of glyphs.

Under iOS 5 and earlier you used to have to do a lot of mental jumping back and forth between Core Foundation C-style objects and Objective-C UIKit objects. As of 6 that's no longer necessary. But be wary if you search the 'net that things got a lot easier under 6 — if you see lots of __bridge casts and manual CFReleases then you're probably looking at older code.

Anyway, supposing you currently have something like:

UILabel *label = [cancelButton titleLabel];

UIFont *font = <whatever>;
UIColor *textColour = <whatever>;
NSString *string = <whatever>;

label.text = string;
label.font = font;
label.textColor = textColour;

You'd instead do something more like:

NSAttributedString *attributedString = 
    [[NSAttributedString alloc]
          initWithString:string
          attributes:
              @{
                    NSFontAttributeName : font,
                    NSForegroundColorAttributeName : textColour
              }];

label.attributedText = attributedString;

In your case, also to adjust the overall kerning you'd add:

NSAttributedString *attributedString = 
    [[NSAttributedString alloc]
          initWithString:string
          attributes:
              @{
                    NSFontAttributeName : font,
                    NSForegroundColorAttributeName : textColour,
                    NSKernAttributeName : @(-1.3f)
              }];

label.attributedText = attributedString;

Or whatever kerning value you want to apply. See the various constants at the bottom of the NSAttributedString UIKit Additions Reference for the various other attributes you can apply and which version of iOS they first became available on.

Much later addendum: while still being one of the least Swifty people you'll meet, I think this is the equivalent in Swift:

button.titleLabel?.attributedText =
    NSAttributedString(
        string: string,
        attributes:
        [
            NSFontAttributeName: font,
            NSForegroundColorAttributeName: textColour,
            NSKernAttributeName: -1.3
        ])

OTHER TIPS

    NSAttributedString *cancelButtonAttributedString = [[NSAttributedString alloc]
                                           initWithString:@"Hello"
                                           attributes:
                                           @{
                                             NSKernAttributeName: @(1.5)
                                             }];
    [cancelButton setAttributedTitle:cancelButtonAttributedString forState:UIControlStateNormal];

This is only simple answer for above question

One of the supported Core Text attributes is kCTKernAttributeName, which as of iOS 6 is easier to take advantage of via the UIKit addition NSKernAttributeName. You can use kerning to adjust the horizontal spacing of glyphs.

Kerning is the adjustment of space between two unique letters. Kerning varies between different character pairs. For example, a combination like 'AVA' would have a different kerning between characters than something like 'VVV'

By using NSKernAttributeName, you're actually overriding that custom space adjustment that is built into the font file, setting all the various character pair kern values to the same number, thus breaking the optimal kerning. When applied to an entire string of text, small amounts of kerning the broken spacing is more noticeable. However, high kerning values could push the letter far enough apart to the point that the poor spacing will not be as noticeable.

What you're looking for is Tracking (aka letter spacing), which is the spacing between all the letters in a given block of text. Unfortunately, iOS does not seem to let you control that attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top