由 CyanHall.com
创建于 2020-12-22,
上次更新:2021-03-30。
👉
如果有用请点赞。
来自 StackOverflow: How to Increase Line spacing in UILabel in Swift
👉
在界面编辑器中
:
在 Swift 4 或 Swift 5 代码中设置
: extension NSAttributedString {
func withLineSpacing(_ spacing: CGFloat) -> NSAttributedString {
let attributedString = NSMutableAttributedString(attributedString: self)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineBreakMode = .byTruncatingTail
paragraphStyle.lineSpacing = spacing
attributedString.addAttribute(.paragraphStyle,
value: paragraphStyle,
range: NSRange(location: 0, length: string.count))
return NSAttributedString(attributedString: attributedString)
}
}
let example = NSAttributedString(string: "This is Line 1 \nLine 2 \nLine 3 ").withLineSpacing(15)
testLabel.attributedText = example
更多