UILabel改变行间距、字间距(swift)

更新时间:2023-05-09 05:17:05 阅读: 评论:0

UILabel改变⾏间距、字间距(swift)
有时候根据需求需要改变label字与字或⾏与⾏之间间距⼤⼩,但系统并没有提供这样的⽅法,下⾯我对label写了⼀个延展来实现这⼀功能!
extension UILabel {
/**  改变⾏间距  */
func changeLineSpace(space:CGFloat) {
== nil || lf.text == "" {
return
}
let text = lf.text
let attributedString = NSMutableAttributedString.init(string: text!)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = space
attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: .init(location: 0, length: (text?.length())!))
lf.attributedText = attributedString
lf.sizeToFit()
}
/**  改变字间距  */
func changeWordSpace(space:CGFloat) {
== nil || lf.text == "" {
return
}
let text = lf.text
let attributedString = NSMutableAttributedString.init(string: text!, attributes: [NSKernAttributeName:space])
let paragraphStyle = NSMutableParagraphStyle()
attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: .init(location: 0, length: (text?.length())!))
lf.attributedText = attributedString
lf.sizeToFit()
}
/**  改变字间距和⾏间距  */
func changeSpace(lineSpace:CGFloat, wordSpace:CGFloat) {
== nil || lf.text == "" {
return
}
let text = lf.text
let attributedString = NSMutableAttributedString.init(string: text!, attributes: [NSKernAttributeName:wordSpace])
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpace
attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: .init(location: 0, length: (text?.length())!))
lf.attributedText = attributedString
lf.sizeToFit()
}
赶快试⼀下吧!
}

本文发布于:2023-05-09 05:17:05,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/561933.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:间距   实现   改变   系统   没有   提供   延展
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图