我正在> = iOS6中创建一个应用。我想改变UILabel中的字符间距。我在我的应用程序中添加了自定义字体“FUTURABT HEAVY”但字符太靠近了。
我找到了好的代码here来增加字符间距。但是如果我试图改变它而不是我的文本变成左对齐而不是中心。
请帮我解决这个问题。
答案 0 :(得分:77)
您应该将NSAttributedString
与NSKernAttributeName
属性
这是一个小例子:
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];
答案 1 :(得分:58)
此快速扩展
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
}
}
所以你可以使用它
MyLabel.addCharactersSpacing(5, text: "Some Text")
答案 2 :(得分:11)
Swift 4
1|20 first range is starting with zero and ending on starting + length
6|40 second range starting with end of the last range
Swift 3
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
}
}
答案 3 :(得分:2)
以下是带有方便扩展的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
}
}
答案 4 :(得分:0)
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;
答案 5 :(得分:0)
带有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
}
}
}