iOS 8 – change character spacing on UILabel within Interface Builder

7 Solutions Collect From Internet About “iOS 8 – change character spacing on UILabel within Interface Builder”

I know it’s not an Interface Builder solution, but you can create a UILabel extension and then add spacing to any UILabel you want:

extension UILabel {
  func addTextSpacing() {
    if let textString = text {
      let attributedString = NSMutableAttributedString(string: textString)
      attributedString.addAttribute(NSKernAttributeName, value: 1.15, range: NSRange(location: 0, length: attributedString.length - 1))
      attributedText = attributedString
    }
  }
}

and then call it after setting the text value:

myLabel.text = "We used to be so close"
myLabel.addTextSpacing()

Consider changing the 1.15 spacing value to something that works for you and the font you use in your app.

Ended up using this for now to get existing attributed text and modify to add character spacing:

let attributedString = discoveryTitle.attributedText as NSMutableAttributedString
attributedString.addAttribute(NSKernAttributeName, value: 1.0, range: NSMakeRange(0, attributedString.length))
discoveryTitle.attributedText = attributedString

Swift 3:

let attributedString = NSMutableAttributedString(string: discoveryTitle.text)
attributedString.addAttribute(NSKernAttributeName, value: CGFloat(1.0), range: NSRange(location: 0, length: attributedString.length))
discoveryTitle.attributedText = attributedString

Using NSRange instead of NSMakeRange works in Swift 3.

For completely static text, like the header of a view or especially the launchScreen, you can insert letters that take up a tiny amount of width (e.g. the ‘l’ character) with 0 opacity. Alternatively set its color to the same as background.

I am aware of the fact, that is not the prettiest solution, but it works without writing any code and does the job, until you can do it by specifying the attributes in Xcode.

The result
How to

Edit / Additional idea: To make your spacing even more variable you can change the font size of the filling charachters in between. (Thanks to @mohamede1945 for that idea)

try this!!

create CustomLabel class

@interface CustomLabel : UILabel
@property (assign, nonatomic) CGFloat myLineSpacing;
@end


@implementation CustomLabel

- (void)setMyLineSpacing:(CGFloat)myLineSpacing {
    _myLineSpacing = myLineSpacing;
    self.text = self.text;
}

- (void)setText:(NSString *)text {
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    paragraphStyle.lineSpacing = _myLineSpacing;
    paragraphStyle.alignment = self.textAlignment;
    NSDictionary *attributes = @{NSParagraphStyleAttributeName: paragraphStyle};
    NSAttributedString *attributedText = [[NSAttributedString alloc] initWithString:text
                                                                         attributes:attributes];
    self.attributedText = attributedText;
}

and set runtime attribute

enter image description here

Note this is actually line spacing (also called leading .. in the very old days (pre-digital) you’d put lead (the metal) between lines to increase the gap between lines. For spacing between letters, that is called kerning .. here’s how to do kerning https://stackoverflow.com/a/21141156/294884

Inspired by budidinho’s answer, this is a more flexible solution for when you want to change the spacing for labels with different line spacings. In stead of going inside the function and changing the value you can just pass it as a parameter.

extension UILabel {
  func setTextSpacingBy(value: Double) {
    if let textString = self.text {
      let attributedString = NSMutableAttributedString(string: textString)
      attributedString.addAttribute(NSKernAttributeName, value: value, range: NSRange(location: 0, length: attributedString.length - 1))
      attributedText = attributedString
    }
  }
}

If you’re trying to do this for your launch screen, just create an image of the text in another software tool. You can’t localize the launch screen anyway so this avoids any hacky approaches.

Change letter spacing in Pages and copy/paste it to Interface Builder
enter image description here
enter image description here