ios 如何增加 UILabel 中的字符间距

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20580736/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:46:09  来源:igfitidea点击:

How to increase the character spacing in UILabel

iosuilabelcharacter-spacing

提问by Dilip

I am creating an app in >=iOS6. And I want to change the character spacing in UILabel. I have added the custom font "FUTURABT HEAVY" in my app but the Character are too close to eachother.

我正在>=iOS6 中创建一个应用程序。我想更改 UILabel 中的字符间距。我在我的应用程序中添加了自定义字体“FUTURABT HEAVY”,但字符彼此太接近。

I have find the good code hereto increase the character spacing. But if i tried to change it than my text became left- align in stead of center.

我在这里找到了增加字符间距的好代码。但是如果我试图改变它,那么我的文本就会变成左对齐而不是居中。

Please help me with this situation.

请帮助我解决这种情况。

回答by B.S.

You should probably use NSAttributedStringwith NSKernAttributeNameattribute

您可能应该使用NSAttributedStringwithNSKernAttributeName属性

Here is a small example:

这是一个小例子:

UILabel *label = [[UILabel alloc] initWithFrame:self.view.bounds];

NSString *string = @"Some important text";
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:string];

float spacing = 5.0f;
[attributedString addAttribute:NSKernAttributeName
            value:@(spacing)
            range:NSMakeRange(0, [string length])];

label.attributedText = attributedString;
[self.view addSubview:label];

回答by Steve

Swift extension for this

为此的 Swift 扩展

extension UILabel {
    func addCharactersSpacing(spacing:CGFloat, text:String) {
        let attributedString = NSMutableAttributedString(string: text)
        attributedString.addAttribute(NSKernAttributeName, value: spacing, range: NSMakeRange(0, text.characters.count))
        self.attributedText = attributedString
    }
}

So you can use it

所以你可以使用它

MyLabel.addCharactersSpacing(5, text: "Some Text")

回答by Krunal

Swift 4

斯威夫特 4

extension UILabel {

   func setCharacterSpacing(characterSpacing: CGFloat = 0.0) {

        guard let labelText = text else { return }

        let attributedString: NSMutableAttributedString
        if let labelAttributedText = attributedText {
            attributedString = NSMutableAttributedString(attributedString: labelAttributedText)
        } else {
            attributedString = NSMutableAttributedString(string: labelText)
        }

        // Character spacing attribute
        attributedString.addAttribute(NSAttributedStringKey.kern, value: characterSpacing, range: NSMakeRange(0, attributedString.length))

        attributedText = attributedString
    }        

}

Swift 3

斯威夫特 3

let label = UILabel()
let stringValue = "Sample text"
let attrString = NSMutableAttributedString(string: stringValue)
attrString.addAttribute(NSKernAttributeName, 2: style, range: NSRange(location: 0, length: stringValue.characters.count))
label.attributedText = attrString

回答by Randy

Swift 4.2 with UILabel extension and @IBInspectable

带有 UILabel 扩展和 @IBInspectable 的 Swift 4.2

extension UILabel {
@IBInspectable var letterSpacing: CGFloat {
    get {
        var range:NSRange = NSMakeRange(0, self.text?.count ?? 0)
        let nr = self.attributedText?.attribute(NSAttributedString.Key.kern, at: 0, effectiveRange: &range) as! NSNumber
        return CGFloat(truncating: nr)
    }

    set {
        let range:NSRange = NSMakeRange(0, self.text?.count ?? 0)

        let attributedString = NSMutableAttributedString(string: self.text ?? "")
        attributedString.addAttribute(NSAttributedString.Key.kern, value: newValue, range: range)
        self.attributedText = attributedString
    }
}

}

}

回答by Medhi

Here the code for Swift 5 with a handy extension :

这是带有方便扩展的 Swift 5 代码:

extension UILabel {
    func addCharactersSpacing(spacing: CGFloat, txt: String) {
        let attributedString = NSMutableAttributedString(string: txt)
        attributedString.addAttribute(NSAttributedString.Key.kern, value: spacing, range: NSRange(location: 0, length: txt.count))
        self.attributedText = attributedString
    }
}

回答by Rakesh Purohit

   NSString *strDigit= @"001";
   NSString *strCrushImpact =[NSStringstringWithFormat:@"%d",[strDigit intValue]];

       // Set space in between character
   float spacing = 3.0f;

   NSMutableAttributedString *attributedStrDigit = [[NSMutableAttributedString alloc] initWithString:strWin];
   [strCrushImpact addAttribute:NSKernAttributeName value:@(spacing)
             range:NSMakeRange(0, [strDigit length])];
 label.attributedText = attributedStrDigit;