How to increase the character spacing in UILabel
Asked Answered
C

6

50

I am creating an app in >=iOS6. And I want to change the character spacing in UILabel. I have added the custom font "FUTURABT HEAVY" in my app but the Character are too close to eachother.

I have find the good code here to increase the character spacing. But if i tried to change it than my text became left- align in stead of center.

Please help me with this situation.

Caldeira answered 14/12, 2013 at 7:17 Comment(0)
P
84

You should probably use NSAttributedString with NSKernAttributeName attribute

Here is a small example:

UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];

NSString *string = @"Some important text";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];

float spacing = 5.0f;
[attributedString addAttribute:NSKernAttributeName
            value:@(spacing)
            range:NSMakeRange(0, [string length])];

label.attributedText = attributedString;
[self.view addSubview:label];
Pompous answered 14/12, 2013 at 9:6 Comment(4)
If you want to set it from Interface Builder, add a UILabel subclass, override setText: method and then set MyLabel as the name of the class for your labels in Interface Builder. It works! :)Foetor
tried it, solution works, however in my case I need to center the label and auto layout works incorrectlyBaird
If anyone needs a solution, check it here: https://mcmap.net/q/282264/-how-to-set-kerning-in-iphone-uilabelVibraculum
But I check It still not effect on Title of navigation bar, why?Knitwear
I
65

Swift extension for this

extension UILabel {
    func addCharactersSpacing(spacing:CGFloat, text:String) {
        let attributedString = NSMutableAttributedString(string: text)
        attributedString.addAttribute(NSAttributedString.Key.kern, value: spacing, range: NSMakeRange(0, text.count-1))
        self.attributedText = attributedString
    }
}

So you can use it

MyLabel.addCharactersSpacing(5, text: "Some Text")
Inge answered 21/12, 2015 at 20:6 Comment(2)
Thanx for adding swift code, it will definitely help some one. upvote for you.Caldeira
Thanks for your help! There was extra space on the end of the string (which in my case was causing a horizontal centering issue), but I fixed it by changing the range to NSMakeRange(0, text.characters.count - 1)Overanxious
N
13

Swift 4

extension UILabel {

   func setCharacterSpacing(characterSpacing: CGFloat = 0.0) {

        guard let labelText = text else { return }

        let attributedString: NSMutableAttributedString
        if let labelAttributedText = attributedText {
            attributedString = NSMutableAttributedString(attributedString: labelAttributedText)
        } else {
            attributedString = NSMutableAttributedString(string: labelText)
        }

        // Character spacing attribute
        attributedString.addAttribute(NSAttributedStringKey.kern, value: characterSpacing, range: NSMakeRange(0, attributedString.length))

        attributedText = attributedString
    }        

}

Swift 3

let label = UILabel()
let stringValue = "Sample text"
let attrString = NSMutableAttributedString(string: stringValue)
attrString.addAttribute(NSKernAttributeName, 2: style, range: NSRange(location: 0, length: stringValue.characters.count))
label.attributedText = attrString
Nyberg answered 25/7, 2017 at 11:36 Comment(0)
K
3

Here the code for Swift 5 with a handy extension :

extension UILabel {
    func addCharactersSpacing(spacing: CGFloat, txt: String) {
        let attributedString = NSMutableAttributedString(string: txt)
        attributedString.addAttribute(NSAttributedString.Key.kern, value: spacing, range: NSRange(location: 0, length: txt.count))
        self.attributedText = attributedString
    }
}
Kinsman answered 7/4, 2020 at 16:28 Comment(0)
A
1

Swift 4.2 with UILabel extension and @IBInspectable

extension UILabel {
@IBInspectable var letterSpacing: CGFloat {
    get {
        var range:NSRange = NSMakeRange(0, self.text?.count ?? 0)
        let nr = self.attributedText?.attribute(NSAttributedString.Key.kern, at: 0, effectiveRange: &range) as! NSNumber
        return CGFloat(truncating: nr)
    }

    set {
        let range:NSRange = NSMakeRange(0, self.text?.count ?? 0)

        let attributedString = NSMutableAttributedString(string: self.text ?? "")
        attributedString.addAttribute(NSAttributedString.Key.kern, value: newValue, range: range)
        self.attributedText = attributedString
    }
}

}

Astrakhan answered 7/9, 2019 at 19:11 Comment(0)
C
0
   NSString *strDigit= @"001";
   NSString *strCrushImpact =[NSStringstringWithFormat:@"%d",[strDigit intValue]];

       // Set space in between character
   float spacing = 3.0f;

   NSMutableAttributedString *attributedStrDigit = [[NSMutableAttributedString alloc] initWithString:strWin];
   [strCrushImpact addAttribute:NSKernAttributeName value:@(spacing)
             range:NSMakeRange(0, [strDigit length])];
 label.attributedText = attributedStrDigit;
Concierge answered 5/9, 2016 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.