如何在iPhone UILabel中设置字距调整

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)

  • 由于属性字符串在逐个字符的基础上处理格式而不是字符串基础,因此从技术上讲它实际上是字距调整. (6认同)
  • 这不是字距调整.这是跟踪. (5认同)
  • Apple专门使用'Kern'来描述这种字母间距行为.Apple的内部API是否真的设置跟踪而不是字距调整?也许.无论如何,似乎这将为寻找此类文本格式的人们提供一个很好的解决方案. (3认同)
  • 当然.但均匀增加空间是字母间距或跟踪.Kerning是特定于字母对的.Apple正在使用错误的术语. (3认同)

And*_*ber 22

之前:

之前

后:

cafter

这是一个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往往在实践中运作良好.

  • 如果您在故事板中设置字距,然后在代码中更改标签文本,则字距不会应用于新文本。您可以在代码中重新应用字距调整,这将使其工作,或者您可以子类化 UILabel 并覆盖 setText 以重新应用字距调整(如果已设置)。 (2认同)

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)


Cod*_*ide 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()
Run Code Online (Sandbox Code Playgroud)


Eoi*_*oin 1

据我所知,UILabel不会渲染 的特性NSAttributedString。有一些不错的开源解决方案。我最近使用TTTAttributedLabel作为接受 NSAttributedString 的 UILabel 的替代品。

DTCoreText(以前的 NSAttributedString+HTML)最近也引起了一些关注。