UILabel的问题
1、设置行间距导致的问题
func setLineSpace(lineSpace: CGFloat = spaceLine) -> NSAttributedString {问题描述,设置行间距后,当内容包含中文时,且UIlabel只有一行时,UILabel的下边距将会被加大,导致显示不对。
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
}
解决方案
判断是否为一行同时包含中文文字,如果是则高度需要处理
var height =label.sizeThatFits(size)当然这样的计算,结果也不完全正确,但是测试效果可以接受
if line == 1 && containHZ {
height = height - label.font.lineHeight + spaceLineHeight
}
2、文字上边距问题
UI的设计图中,文字有上边距如:
文章图片
但实际上,UILabel无法设计上边距,目前只能通过慢慢调整来配合UI
3、加入图片后的富文本高度计算不对
【UILabel的问题】加入后图片的富文本调整每行文字的高度时,此时设置numberOfLine = 0,行数计算可能不对,此时可以用UITextView替换UILabel。大多数UILabel的问题都可以使用UITextView替换解决
推荐阅读
- 热闹中的孤独
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- 放屁有这三个特征的,请注意啦!这说明你的身体毒素太多
- 一个人的旅行,三亚
- 布丽吉特,人生绝对的赢家
- parallels|parallels desktop 解决网络初始化失败问题
- 慢慢的美丽
- 尽力
- 一个小故事,我的思考。
- 家乡的那条小河