如何在Swift中增加UILabel的行间距
问题描述:
我有一个标签,文本行很少,我想增加行之间的间距。其他人也提出了类似的问题,但解决方案并没有解决我的问题。我的标签也可能包含或不包含段落。我是 Swift
的新手。是否有使用故事板的解决方案?或者只通过 NSAttributedString
可能吗?
I have a label which has few lines of text and I want to increase the spacing between the lines. There are similar questions asked by others but the solutions don't solve my problems. Also my label may or may not contain paragraphs. I am new to Swift
. Is there a solution using storyboard? Or only through NSAttributedString
its possible?
答
以编程方式将LineSpacing添加到您的 UILabel
使用以下代码段。
Programatically add LineSpacing to your UILabel
using following snippet.
let attributedString = NSMutableAttributedString(string: "Your text")
// *** Create instance of `NSMutableParagraphStyle`
let paragraphStyle = NSMutableParagraphStyle()
// *** set LineSpacing property in points ***
paragraphStyle.lineSpacing = 2 // Whatever line spacing you want in points
// *** Apply attribute to string ***
attributedString.addAttribute(NSParagraphStyleAttributeName, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
// *** Set Attributed String to your label ***
label.attributedText = attributedString;