如何在Swift中的UILabel中增加行距
我有一个只有几行文字的标签,我想增加两行之间的间距。其他人也有类似的问题,但是解决方案不能解决我的问题。另外,我的标签可能包含也可能不包含段落。我是新来的Swift
。有使用情节提要的解决方案吗?还是只能通过NSAttributedString
其可能?
回答:
UILabel
使用以下代码段将LineSpacing编程添加到您的代码中。
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
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(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
// *** Set Attributed String to your label ***
label.attributedText = attributedString
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(NSAttributedString.Key.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attributedString.length))
// *** Set Attributed String to your label ***
label.attributedText = attributedString
以上是 如何在Swift中的UILabel中增加行距 的全部内容, 来源链接: utcz.com/qa/400022.html