#WITHYOU
Lum7671's Weblog
#WITHYOU

iOS, UILabel 에서 글자 간격 조정하기.

NSAttributedString 의 NSKernAttributeName 설정

1. 소스

@interface ViewController ()
@property (assign, nonatomic) BOOL toggle;
@end

@implementation ViewController

#define LABEL_TEXT @"MAGENTA"
#define SMALL_FLOAT 0.0f
#define LARGE_FLOAT 20.0f

- (IBAction)clickedTestButton:(id)sender {

    _toggle = (_toggle)?NO:YES;

    NSString *tmpStr = @"";
    NSNumber *tmpNum = nil;
    if (_toggle) {
        tmpStr = [NSString stringWithFormat:@"%@\n%0.0f", LABEL_TEXT, LARGE_FLOAT];
        tmpNum = @(LARGE_FLOAT);
    } else {
        tmpStr = [NSString stringWithFormat:@"%@\n%0.0f", LABEL_TEXT, SMALL_FLOAT];
        tmpNum = @(SMALL_FLOAT);
    }

    NSDictionary *attrDict = @{NSKernAttributeName:tmpNum, // ※ 글자 사이 간격
                               NSFontAttributeName:[UIFont  boldSystemFontOfSize:30.0f],
                               NSForegroundColorAttributeName:[UIColor magentaColor]};

    NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString:tmpStr attributes:attrDict];
    _testLabel.attributedText = attributedString ;
}
@end

2. iPhone 스샷

2.1 kern 0

kern 0

2.2 kern 20

kern 20