I am developing an iPhone app, and I want to set kerning in UILabel. The code I\'ve written (possibly around kCTKernAttributeName
) seems to be in error. How mig
Taking DBD's answer, I made a category on UILabel which allows setting the kerning if running on iOS6+ with graceful fall back to just setting text on previous iOS versions. Might be of help to others...
UILabel+TextKerning.h
#import
@interface UILabel (TextKerning)
/**
* Set the label's text to the given string, using the given kerning value if able.
* (i.e., if running on iOS 6.0+). The kerning value specifies the number of points
* by which to adjust spacing between characters (positive values increase spacing,
* negative values decrease spacing, a value of 0 is default)
**/
- (void) setText:(NSString *)text withKerning:(CGFloat)kerning;
/**
* Set the kerning value of the currently-set text. The kerning value specifies the number of points
* by which to adjust spacing between characters (positive values increase spacing,
* negative values decrease spacing, a value of 0 is default)
**/
- (void) setKerning:(CGFloat)kerning;
@end
UILabel+TextKerning.m
#import "UILabel+TextKerning.h"
@implementation UILabel (TextKerning)
-(void) setText:(NSString *)text withKerning:(CGFloat)kerning
{
if ([self respondsToSelector:@selector(setAttributedText:)])
{
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:text];
[attributedString addAttribute:NSKernAttributeName
value:[NSNumber numberWithFloat:kerning]
range:NSMakeRange(0, [text length])];
[self setAttributedText:attributedString];
}
else
[self setText:text];
}
-(void) setKerning:(CGFloat)kerning
{
[self setText:self.text withKerning:kerning];
}