UI的绘图功能是在视图尺寸确定以后,将信息和样式绘制的过程;
给信息和样式的修改提供的最后的机会。
UILabel 控制行段间距的终极代码。
class LineSpaceLabel: UILabel {
var lineSpace = 2.0
var paragraphSpace = 0.0
override func draw(_ rect: CGRect) {
/*
paragraphStyle.lineSpacing = 10;// 字体的行间距
paragraphStyle.firstLineHeadIndent = 20.0f;//首行缩进
*/
var attributeString = NSMutableAttributedString(string:self.text ?? "")
if let attrStr = self.attributedText
{
attributeString = NSMutableAttributedString.init(attributedString: attrStr)
}
let paraStyle = NSMutableParagraphStyle()
paraStyle.lineSpacing = CGFloat(lineSpace) //行间距
paraStyle.paragraphSpacing = CGFloat(paragraphSpace)
attributeString.addAttribute(NSAttributedStringKey.paragraphStyle,
value: paraStyle,
range: attributeString.rangeOfSelf())
self.attributedText = attributeString
super.draw(rect)
}
}