Search code examples
iosuilabelcharacter-spacing

How to increase the character spacing in UILabel


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.


Solution

  • 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];