如何更改UILabel中的字母间距?

Ben*_*mas 17 spacing uilabel ios

我想改变a中数字之间的间距UIKit UILabel,使其相等.

使用标准间距,标签如下所示:

字符间距不均匀的标签

我希望它看起来像这样:

带有均匀字符间距的标签

怎么能实现这一目标?

J2K*_*J2K 24

您可以NSKernAttributeName属性字符串上使用该属性:

UILabel *label = [UILabel new];

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] 
                                   initWithString:@"127"];

// The value paramenter defines your spacing amount, and range is 
// the range of characters in your string the spacing will apply to. 
// Here we want it to apply to the whole string so we take it from 0 to text.length.
[text addAttribute:NSKernAttributeName 
             value:@-0.5 
             range:NSMakeRange(0, text.length)];

[label setAttributedText:text];
Run Code Online (Sandbox Code Playgroud)


miO*_*iOS 17

最简单的方法是创建自定义UILabel类并从Storyboard设置字母间距.

open class CustomLabel : UILabel {
    @IBInspectable open var characterSpacing:CGFloat = 1 {
        didSet {
            let attributedString = NSMutableAttributedString(string: self.text!)
            attributedString.addAttribute(NSKernAttributeName, value: self.characterSpacing, range: NSRange(location: 0, length: attributedString.length))
            self.attributedText = attributedString
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


小智 8

在快速实施看起来像

let text = "TIPS, ARTICLES & BEST OFFERS"
label.attributedText = NSAttributedString(string: text, attributes: [.kern: 3.12])
Run Code Online (Sandbox Code Playgroud)

NSAttributedString .kern 属性


小智 6

您可以使用NSAttributedString并使用NSKernAttributeName属性.此默认值为0,因此您需要将其设置为负数.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSAttributedString_Class/#//apple_ref/doc/constant_group/Character_Attributes

在Obj-C中你可以这样做:

NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc]initWithString: @"Test test test test "];
[attrStr addAttribute:NSKernAttributeName value:@(4.0) range:NSMakeRange(0, attrStr.length)];

label.attributedText = attrStr;
Run Code Online (Sandbox Code Playgroud)

在Swift中你可以做这样的事情:

let myTitle = "my title"
let titleLabel = UILabel()
let attributes: NSDictionary = [
    NSFontAttributeName:UIFont(name: "FONT_NAME", size: TEXT_SIZE),
    NSKernAttributeName:CGFloat(2.0)
]

let attributedTitle = NSAttributedString(string: myTitle, attributes:attributes as? [String : AnyObject])

titleLabel.attributedText = attributedTitle
Run Code Online (Sandbox Code Playgroud)


jn-*_*-se 6

NSKernAttributeName可以使用。

但更正其他答案:不适用于全文长度,但(text.length - 1)

将负或正间距添加到字母中,最后一个不需要。假设您要添加一个正间距,它会在最后一个字母之后的间距中结束。居中的字符串似乎不再居中。这同样适用于负间距。

NSString *text = @"Sample Text";    
UILabel *label = [UILabel new]

NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString: text];
[attributedString addAttribute:NSKernAttributeName value:[NSNumber numberWithDouble:-1.0] range:NSMakeRange(0, text.length-1)];

[label setAttributedTitle:attributedString forState:UIControlStateNormal];
Run Code Online (Sandbox Code Playgroud)

应用于全文长度。

应用于全文长度。

应用于(文本长度 - 1)。 应用于(文本长度 - 1)。

  • 非常有帮助的补充!只是一件事:我们在传入范围为 (0, -1) 的空字符串时遇到了问题。所以我添加了 max(0, text.length - 1) 以使其安全。 (2认同)

Pom*_*ule 6

我从@J2K 那里得到了答案并将其翻译成Swift 4.2扩展。

/// Set the text of the label but altering the kerning so that you can control the space between each characters.
///
/// - Parameters:
///   - text: New content of the label
///   - kerning: Set a value between 0 and 1 to lower the space between characters. Above 0, spacing will be increased. 0 disables kerning.
extension UILabel {

    func set(text: String, withKerning kerning: CGFloat) {
        let attributedString = NSMutableAttributedString(string: text)

        // The value parameter defines your spacing amount, and range is
        // the range of characters in your string the spacing will apply to.
        // Here we want it to apply to the whole string so we take it from 0 to text.count.
        attributedString.addAttribute(NSAttributedString.Key.kern, value: kerning, range: NSMakeRange(0, text.count))

        attributedText = attributedString
    }

}
Run Code Online (Sandbox Code Playgroud)