How do I change the letter spacing in a UILabel?

uilabel letter spacing swift
uilabel line spacing
nskernattributename
uilabel line spacing swift
ios letter spacing
uilabel kerning
add line spacing to label in swift
uilabel row space

I want to change the spacing between digits in a UIKit UILabel so that it is equal.

With standard spacing, the label looks like this:

I'd like it to look like this:

How can this be achieved?

You can use the NSKernAttributeName attribute on an attributed string:

UILabel *label = [UILabel new];

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] 
                                   initWithString:@"127"];

// The value paramenter defines your spacing amount, and range is 
// the range of characters in your string the spacing will apply to. 
// Here we want it to apply to the whole string so we take it from 0 to text.length.
[text addAttribute:NSKernAttributeName 
             value:@-0.5 
             range:NSMakeRange(0, text.length)];

[label setAttributedText:text];

How to increase the character spacing in UILabel, 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  You would have to go back to the above Use None to reset to zero and then re-adjust the kerning. In keynote 09 you had sliders for Character Spacing and Line Spacing next to each other which made adjusting text in a fixed size Text Box a breeze. Hope this feature will come back with the next update. It is sorely missed.

The easiest way is to create a custom UILabel class and set the letter spacing from Storyboard.

open class CustomLabel : UILabel {
    @IBInspectable open var characterSpacing:CGFloat = 1 {
        didSet {
            let attributedString = NSMutableAttributedString(string: self.text!)
            attributedString.addAttribute(NSKernAttributeName, value: self.characterSpacing, range: NSRange(location: 0, length: attributedString.length))
            self.attributedText = attributedString
        }

    }
}

Change character spacing on UILabel within Interface Builder , adjustsLetterSpacingToFitWidth. A Boolean value indicating whether the label should adjust spacing between letters to fit the string within the label's bounds  The easiest way is to create a custom UILabel class and set the letter spacing from Storyboard. open class CustomLabel : UILabel { @IBInspectable open var characterSpacing:CGFloat = 1 { didSet { let attributedString = NSMutableAttributedString(string: self.text!) attributedString.addAttribute(NSKernAttributeName, value: self.characterSpacing, range: NSRange(location: 0, length: attributedString.length)) self.attributedText = attributedString } } }

You can use an NSAttributedString and play with the NSKernAttributeName attribute. The default value for this is 0 so you will want to set it to a negative number.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/#//apple_ref/doc/constant_group/Character_Attributes

in Obj-C you can do something like this:

NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc]initWithString: @"Test test test test "];
[attrStr addAttribute:NSKernAttributeName value:@(4.0) range:NSMakeRange(0, attrStr.length)];

label.attributedText = attrStr;

in Swift you could do something like this:

let myTitle = "my title"
let titleLabel = UILabel()
let attributes: NSDictionary = [
    NSFontAttributeName:UIFont(name: "FONT_NAME", size: TEXT_SIZE),
    NSKernAttributeName:CGFloat(2.0)
]

let attributedTitle = NSAttributedString(string: myTitle, attributes:attributes as? [String : AnyObject])

titleLabel.attributedText = attributedTitle

adjustsLetterSpacingToFitWidth, Is it possible to change the space between letters while using UILabel? systemFont(ofSize: 26)) func set(to label: UILabel, with text: String = "") { if var attributes  Step 5: In the "Spacing" box, select "Expanded" if you want to increase the spacing between letters, or select "Condensed" if you want to decrease the spacing, then type the value you want to expand or condense. Step 6: Click "OK" at the bottom to finish. The following is the comparison when the spacing is expanded to 1.5 pt.

I've taken the answer from @J2K and translated it into a Swift 4.2 extension.

/// Set the text of the label but altering the kerning so that you can control the space between each characters.
///
/// - Parameters:
///   - text: New content of the label
///   - kerning: Set a value between 0 and 1 to lower the space between characters. Above 0, spacing will be increased. 0 disables kerning.
extension UILabel {

    func set(text: String, withKerning kerning: CGFloat) {
        let attributedString = NSMutableAttributedString(string: text)

        // The value parameter defines your spacing amount, and range is
        // the range of characters in your string the spacing will apply to.
        // Here we want it to apply to the whole string so we take it from 0 to text.count.
        attributedString.addAttribute(NSAttributedString.Key.kern, value: kerning, range: NSMakeRange(0, text.count))

        attributedText = attributedString
    }

}

Change the space between Letters, In the second method, here's the code to assign spacing between characters. Let's check out how we will use this extension. Select all the text at the bottom of the page that doesn’t quite fit. On the Home tab, click the dialog box launcher in the Font group (. The Font dialog box opens. In the Font dialog box, click the Advanced tab; from the Scale drop-down list, choose 90%, and then click OK.

On swift implementation looks like

let text = "TIPS, ARTICLES & BEST OFFERS"
label.attributedText = NSAttributedString(string: text, attributes: [.kern: 3.12])

Spacing between each character in UILabel | Swift, A common application is to use this to display a string and adding custom kerning / letter-spacing. This would be achieved as follows (where label is a UILabel )  Select the text and and reduce the letter spacing, to get it almost perfect. There will be some letters overlapping each other, but we will fix this in a moment. Select your text and this time click on advanced and select ungroup to letters. Now you will be able to move every letter on its own.

iOS, I want to change the spacing between digits in a UIKit UILabel so that it is equal. With standard spacing, the label looks like this: I'd like it to look like this: How  If wanting to change the spacing between letters, there is the "\T" format code that can be applied from an external editor. I'm not sure how the internal one handles this. Values between 0.75 and 4 as in \T1.25 wrote in message news:5751250@discussion.autodesk.com Is there a variable in acad2006 that controls the spaces between each letter

How do I change the letter spacing in a UILabel?, NSAttributedString / Creating a string that has custom kerning letter spacing / Essential iOS. This would be achieved as follows (where label is a UILabel ), giving a different kerning for the word “kerning” Change the color of a word or string. To automatically adjust the spacing between selected characters based on their shapes, select Optical for the Kerning option in the Character panel. To adjust kerning manually, place an insertion point between two characters, and set the desired value for the Kerning option in the Character panel.

NSAttributedString / Creating a string that has custom kerning letter , This UILabel subclass supports custom character spacing for all unicode symbols​. Its draws letters as glyphs. */. @interface CharSpacingUILabel : UILabel. {. CGFloat How to change the text Alignment. Bcoz as i use the custom label my text  You can increase or decrease the spacing between lines of text, and change the amount of space before or after a paragraph. Select one or more paragraphs, or select the text in a text box, shape, or table. Note: Your changes only apply to the paragraphs or text selected.

Comments
  • Thanks J2K. I forgot about the ability to apply the kerning to a subrange of the string.
  • Default value will be never applied. You should fix the code.
  • That's only the "easiest" way if you use interface builder.
  • The question was not "How can I create an extension to adjust the spacing of a label", merely how to do it. These sort of replies just create noise in StackOverflow.
  • Very helpful addition! Just one thing: we had problems when passing in an empty string with the range being (0, -1). So I added max(0, text.length - 1) to make it safe.
  • can you explain, because I see no option for the "letter spacing"