我正在开发一款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];
答案 0 :(得分:59)
老问题,但你现在可以(轻松)做到。
NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:@"Please get wider"];
[attributedString addAttribute:NSKernAttributeName value:@5 range:NSMakeRange(10, 5)];
[self.label setAttributedText:attributedString];
2013年11月,为了扩展这个好的答案,这里有一些完全典型的代码。通常你也会设置字体。在评论中注意使用普通的旧.text的老式方式。希望它可以帮助某人
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];
答案 1 :(得分:20)
这是一个Swift 2扩展程序,您可以通过代码或 storyboard 设置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 在实践中效果很好。
答案 2 :(得分:18)
考虑到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.
* (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];
}
答案 3 :(得分:6)
为了及时更新,iOS 6为UILabel
和UITextView
引入了referencedText!
答案 4 :(得分:5)
只需在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()
答案 5 :(得分:3)
使用IBDesignables和IBInspectables的示例,您可以设置仅通过故事板设置字距调整值。 我发现它非常实用,我想与你分享。
<强> 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
答案 6 :(得分:1)
据我所知,UILabel
不会呈现NSAttributedString
的特征。有几个不错的开源解决方案。我最近使用TTTAttributedLabel作为替换接受NSAttributedString的UILabel的交换。
DTCoreText(以前的NSAttributedString + HTML)最近也有点嗡嗡声。
答案 7 :(得分:1)
快速4和5
extension NSAttributedString {
/// Returns a new instance of NSAttributedString with same contents and attributes with kerning added.
/// - Parameter kerning: a kerning you want to assign to the text.
/// - Returns: a new instance of NSAttributedString with given kerning.
func withKerning(_ kerning: CGFloat) -> NSAttributedString {
let attributedString = NSMutableAttributedString(attributedString: self)
attributedString.addAttributes([.kern: kerning],
range: NSRange(location: 0, length: string.count))
return NSAttributedString(attributedString: attributedString)
}
]
答案 8 :(得分:-1)
在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设置为attributedText:
即可self.label.attributedText = NSMutableAttributedString(string: "SwiftExample", attributes: attributes("SourceSans-Regular", color: UIColor.whiteColor(), fontSize: 20, kern: 2.0))
显然,我添加了一些你可能不需要的参数。玩弄 - 随意重写方法 - 我在一堆不同的答案上寻找这个,所以我想发布整个扩展,以防有人在那里... -rab