如何在iPhone UILabel中设置字距

我正在开发一个iPhone应用程序,我想在UILabel中设置字距。 我写的代码(可能围绕kCTKernAttributeName )似乎是错误的。 我怎样才能解决这个问题?

 NSMutableAttributedString *attStr; NSString *str = @"aaaaaaa"; CFStringRef kern = kCTKernAttributeName; NSNumber *num = [NSNumber numberWithFloat: 2.0f]; NSDictionary *attributesDict = [NSDictionary dictionaryWithObject:num forKey:(NSString*)kern]; [attStr initWithString:str attributes:attributesDict]; CGRect frame1 = CGRectMake(0, 0, 100, 40); UILabel *label1 = [[UILabel alloc] initWithFrame:frame1]; label1.text = attStr [self.view addSubview:label1]; 

老问题,但你现在可以做到(很容易)。

 NSMutableAttributedString *attributedString; attributedString = [[NSMutableAttributedString alloc] initWithString:@"Please get wider"]; [attributedString addAttribute:NSKernAttributeName value:@5 range:NSMakeRange(10, 5)]; [self.label setAttributedText:attributedString]; 

在这里输入图像说明

2013年11月,为了扩大这个伟大的答案,这里有一些完全典型的代码。 通常你也会设置字体。 在评论中注意使用普通旧文本的老式方式。 希望它可以帮助别人

 NSString *yourText = @"whatever"; UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0,0,0,0)]; // simple approach with no tracking... // label.text = yourText; // [label setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:24]]; NSMutableAttributedString *attributedString; attributedString = [[NSMutableAttributedString alloc] initWithString:yourText]; [attributedString addAttribute:NSKernAttributeName value:[NSNumber numberWithFloat:2.0] range:NSMakeRange(0, [yourText length])]; [attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Light" size:24] range:NSMakeRange(0, [yourText length])]; label.attributedText = attributedString; label.textColor = [UIColor blackColor]; label.backgroundColor = [UIColor clearColor]; label.textAlignment = NSTextAlignmentCenter; [label sizeToFit]; 

考虑到DBD的回答,我在UILabel上做了一个类别,它允许在iOS6 +上运行字距调整,优雅地回落到之前的iOS版本。 可能对别人有帮助…

的UILabel + TextKerning.h

 #import <UIKit/UIKit.h> @interface UILabel (TextKerning) /** * Set the label's text to the given string, using the given kerning value if able. * (ie, 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]; } 

之前:

之前

后:

cafter

这里有一个Swift 2扩展,让你通过代码故事板设置UILabel的字距:

 extension UILabel { @IBInspectable var kerning: Float { get { var range = NSMakeRange(0, (text ?? "").characters.count) guard let kern = attributedText?.attribute(NSKernAttributeName, at: 0, effectiveRange: &range), let value = kern as? NSNumber else { return 0 } return value.floatValue } set { var attText:NSMutableAttributedString if let attributedText = attributedText { attText = NSMutableAttributedString(attributedString: attributedText) } else if let text = text { attText = NSMutableAttributedString(string: text) } else { attText = NSMutableAttributedString(string: "") } let range = NSMakeRange(0, attText.length) attText.addAttribute(NSKernAttributeName, value: NSNumber(value: newValue), range: range) self.attributedText = attText } } } 

演示用法:

 myLabel.kerning = 3.0 

要么

在这里输入图像说明

演示使用了3.0调整,但我发现0.1-0.8在实践中运行良好。

只是为了在这里更新,iOS 6引入了UILabelUITextView属性UITextView

UILabel参考:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UILabel_Class/Reference/UILabel.html#//apple_ref/occ/instp/UILabel/attributedText

只要在Swift中做这个:

  let myTitle = "my title" let titleLabel = UILabel() let attributes: NSDictionary = [ NSFontAttributeName:UIFont(name: "HelveticaNeue-Light", size: 20), NSForegroundColorAttributeName:UIColor.whiteColor(), NSKernAttributeName:CGFloat(2.0) ] let attributedTitle = NSAttributedString(string: myTitle, attributes: attributes as? [String : AnyObject]) titleLabel.attributedText = attributedTitle titleLabel.sizeToFit() 

一个使用IBDesignables和IBInspectables的例子,您可以通过它只pipe理故事板设置字距值。 我发现它非常实用,我想与你分享。

UILabelKerning.h

 #import <UIKit/UIKit.h> IB_DESIGNABLE @interface UILabelKerning : UILabel @property (assign, nonatomic) IBInspectable int kerning; @end 

UILabelKerning.m

 #import "UILabelKerning.h" @implementation UILabelKerning -(void)awakeFromNib { [self setTheAttributes]; } - (id)initWithCoder:(NSCoder*)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { // Initialization code } return self; } -(void)setTheAttributes{ NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithAttributedString:self.attributedText]; [attributedString addAttribute:NSKernAttributeName value:[NSNumber numberWithFloat:self.kerning] range:NSMakeRange(0, [self.text length])]; [self setAttributedText:attributedString]; } @end 

在这里输入图像说明

在这里输入图像说明

在这里输入图像说明

据我所知, UILabel将不呈现NSAttributedString的特征。 有几个很好的开源解决scheme。 我最近使用TTTAttributedLabel作为replace接受NSAttributedString的UILabel的交换。

DTCoreText (以前的NSAttributedString + HTML)最近也有一些嗡嗡声。

在Swift 2.0中…

添加一个扩展名:

 extension UIView { func attributes(font: String, color: UIColor, fontSize: CGFloat, kern: Double) -> [String: NSObject] { let attribute = [ NSForegroundColorAttributeName: color, NSKernAttributeName: kern, NSFontAttributeName : UIFont(name: font, size: fontSize)! ] return attribute } } 

现在,只需将你的UILabel设置为属性文本:

 self.label.attributedText = NSMutableAttributedString(string: "SwiftExample", attributes: attributes("SourceSans-Regular", color: UIColor.whiteColor(), fontSize: 20, kern: 2.0)) 

显然,我添加了一堆你可能不需要的参数。 玩耍 – 随意重写方法 – 我正在寻找一堆不同的答案,所以我想张贴整个扩展,以防止有人在那里… -rab