簡體   English   中英

如何在UILabel中設置字符和行之間的空格

[英]How to set a space between characters and lines in UILabel

我想在第一行和第二行之間設置一個間距,在其他行之間需要另一個間距。 有了這個,第二行和下一行必須具有特定的字符間距。

這一切都需要在一個控件中完成。 我怎么能這樣做? 我決定為每一行創建一個單獨的UILabel ,但我認為這是錯誤的方式。

您無法更改文本行之間的間距,您必須子類化UILabel並滾動您自己的drawTextInRect,創建多個標簽或使用不同的字體。

但是有兩個自定義標簽,允許您控制線高。

1) https://github.com/LemonCake/MSLabel

2) https://github.com/Tuszy/MTLabel

希望這可以幫助...

在iOS6中,您可以這樣做:

NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:40];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
cell.label.attributedText = attributedString ;

試試這個,它應該適合你(使用Swift 4)

let label = UILabel()
let stringValue = "How to\ncontrol\nthe\nline spacing\nin UILabel"
let attrString = NSMutableAttributedString(string: stringValue)
var style = NSMutableParagraphStyle()
style.lineSpacing = 24 // change line spacing between paragraph like 36 or 48
style.minimumLineHeight = 20 // change line spacing between each line like 30 or 40
attrString.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: stringValue.characters.count))
// add strike
attrString.addAttribute(NSAttributedStringKey.strikethroughStyle, value: 2, range: NSMakeRange(0, attrString.length))
// add space between characters
attrString.addAttribute(NSAttributedStringKey.kern, value: 2, range: NSMakeRange(0, attrString.length))
label.attributedText = attrString


結果:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM