美文网首页
UILabel的问题

UILabel的问题

作者: limbo_lyn | 来源:发表于2020-04-24 10:34 被阅读0次

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无法设计上边距,目前只能通过慢慢调整来配合UI

3、加入图片后的富文本高度计算不对

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

相关文章

网友评论

      本文标题:UILabel的问题

      本文链接:https://www.haomeiwen.com/subject/icexwhtx.html