2016-06-07 103 views

回答

9

您可以使用一個屬性串的NSKernAttributeName屬性:

UILabel *l = [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:[NSNumber numberWithDouble:-0.5] range:NSMakeRange(0, text.length)]; 
[l setAttributedText:text]; 
+0

謝謝J2K。我忘記了將字距應用於字符串的子範圍的能力。 –

1
NSString *myString = @"127"; 
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:myString]; 

float letterSpacing = -1.50f; // change spacing here 
[attributedString addAttribute:NSKernAttributeName value:@(letterSpacing) range:NSMakeRange(0, [myString length])]; 
[myLabel setAttributedText:attributedString]; 

也看到了更多的信息和結果:http://www.devsign.co/notes/tracking-and-character-spacing

0

單擊標籤,然後轉到您的屬性檢查器。將文本從純文本更改爲歸因。你會有幾種間距選項。希望這可以幫助。

+0

你能解釋一下嗎,因爲我看不到「字母間距」的選項 –

5

您可以使用NSAttributedString並播放與NSKernAttributeName屬性。這個的默認值是0,所以你需要將它設置爲負數。

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

的對象 - 你可以做這樣的事情:

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

label.attributedText = attrStr; 

斯威夫特,你可以做這樣的事情:

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 
15

最簡單的方法是創建一個自定義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 
     } 

    } 
} 

enter image description here

2

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]; 

應用於全文的長度。

​​

應用於(文本長度 - 1)。 Applied to (text length - 1).