我想在第一行和第二行之间设置一个间距,其他行之间需要另一个间距。有了这个,第二行和下一行必须有特定的字符间距。
这一切都需要在一个控制中完成。我怎么能这样做?我决定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
结果: