kz0*_*011 32 cocoa-touch uilabel kerning ios
我正在开发一个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];
Run Code Online (Sandbox Code Playgroud)
DBD*_*DBD 60
老问题,但你现在可以做到(很容易).
NSMutableAttributedString *attributedString;
attributedString = [[NSMutableAttributedString alloc] initWithString:@"Please get wider"];
[attributedString addAttribute:NSKernAttributeName value:@5 range:NSMakeRange(10, 5)];
[self.label setAttributedText:attributedString];
Run Code Online (Sandbox Code Playgroud)

对于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];
Run Code Online (Sandbox Code Playgroud)
And*_*ber 22
这是一个Swift 2扩展,让你通过代码或故事板设置UILabel的字距:
extension UILabel {
@IBInspectable var kerning: Float {
get {
var range = NSMakeRange(0, (text ?? "").count)
guard let kern = attributedText?.attribute(NSAttributedStringKey.kern, 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(NSAttributedStringKey.kern, value: NSNumber(value: newValue), range: range)
self.attributedText = attText
}
}
}
Run Code Online (Sandbox Code Playgroud)
myLabel.kerning = 3.0
Run Code Online (Sandbox Code Playgroud)
要么
该演示使用3.0字距调整戏剧,但我发现0.1 - 0.8往往在实践中运作良好.
Jef*_*Hay 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
Run Code Online (Sandbox Code Playgroud)
的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];
}
Run Code Online (Sandbox Code Playgroud)
为了在这里获得最新信息,iOS 6引入了belongsText for UILabel和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()
Run Code Online (Sandbox Code Playgroud)
据我所知,UILabel不会渲染 的特性NSAttributedString。有一些不错的开源解决方案。我最近使用TTTAttributedLabel作为接受 NSAttributedString 的 UILabel 的替代品。
DTCoreText(以前的 NSAttributedString+HTML)最近也引起了一些关注。
| 归档时间: |
|
| 查看次数: |
30858 次 |
| 最近记录: |