UILabel的问题

1、设置行间距导致的问题

func setLineSpace(lineSpace: CGFloat = spaceLine) -> NSAttributedString {
let mutableString = NSMutableAttributedString(string: self)
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = lineSpace
mutableString.addAttributes([NSAttributedString.Key.paragraphStyle : paragraphStyle], range: NSRange(location: 0, length: self.count))return mutableString
}
问题描述,设置行间距后,当内容包含中文时,且UIlabel只有一行时,UILabel的下边距将会被加大,导致显示不对。
解决方案
判断是否为一行同时包含中文文字,如果是则高度需要处理
var height =label.sizeThatFits(size)
if line == 1 && containHZ {
height = height - label.font.lineHeight + spaceLineHeight
}
当然这样的计算,结果也不完全正确,但是测试效果可以接受


2、文字上边距问题
UI的设计图中,文字有上边距如:


UILabel的问题
文章图片
但实际上,UILabel无法设计上边距,目前只能通过慢慢调整来配合UI


3、加入图片后的富文本高度计算不对
【UILabel的问题】加入后图片的富文本调整每行文字的高度时,此时设置numberOfLine = 0,行数计算可能不对,此时可以用UITextView替换UILabel。大多数UILabel的问题都可以使用UITextView替换解决

    推荐阅读