5
A
回答
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];
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,所以你需要將它設置爲負數。
的對象 - 你可以做這樣的事情:
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
}
}
}
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. UILabel如何擰緊字母間距
- 2. System.Drawing.Font - 更改字母間距?
- 3. 如何更改Swift中的字母間距?
- 4. 如何在Swift中更改UIButton的字母間距?
- 5. 如何更改核心文本中的字母間距/跟蹤?
- 6. 更改字母間距和字間距與jQuery
- 7. UILabel中的字間距
- 8. 如何更改UILabel行中的垂直邊距/間隙
- 9. 如何修改JTextPane中的字母間距?
- 10. 如何更改字體的間距(字距)?
- 11. 子類的UILabel有字母間距不口音信合作
- 12. 是否可以設置字母之間的UILabel距離?
- 13. Zend_Pdf字母間距
- 14. java.awt.font字母間距
- 15. UILabel行間距
- 16. 如何更改DataForm中字段之間的間距?
- 17. 如何在文本框中更改XAML中的字符間距?
- 18. 如何更改UIlabel的字體?
- 19. 如何更改uilabel的字體?
- 20. Android的EditText中的字母間距
- 21. 更改字體屬性以更改字符串中字符之間的間距?
- 22. 如何更改單詞和字符之間的間距?
- 23. 如何更改phpword中的字體(名稱,大小,行間距)?
- 24. Wordpress中的SVG字母間距
- 25. CSS中的行和字母間距
- 26. CStatic控件中的字母間距?
- 27. Abcpdf 9和字母間距
- 28. C#抽繩字母間距
- 29. 在TextView中更改文字字距或間距?
- 30. 如何刪除CSS中元素最後一個字母的字母間距?
謝謝J2K。我忘記了將字距應用於字符串的子範圍的能力。 –