1.需求
2.解决方案
UILabel中有attributedString属性,NSAttributedString有NSTextAttachment属性可以支持在文本中插入图片,从而达到在文本中插入图片的效果
3.代码
let attch = NSTextAttachment()
attch.image = UIImage(named:"icon_warning")
attch.bounds = CGRect(x:0, y:-3, width:14, height:14)
let attr1 = NSMutableAttributedString(attachment: attch)
let spaceStr = NSMutableAttributedString(string:" ")
let attr = NSMutableAttributedString()
attr.insert(attr1, at:0)
attr.append(spaceStr)
let att2 = NSAttributedString(string:"流拍超三次",
attributes: [.foregroundColor: UIColor.WD.mainTertiary,
.font: UIFont.mediumSystemFont(ofSize:12)])
let att3 = NSAttributedString(string: ": 平台即将对流拍多次的物品, 收取一定金额的仓储费或上拍费, 建议您调低价格尽快出售!",
attributes: [.foregroundColor: UIColor.WD.main,
.font: UIFont.WD.regular_12])
attr.append(att2)
attr.append(att3)
self.unsaleLab.attributedText = attr
4.为什么需要空格间隙
因为在插入图片之后,文字会紧贴着图片,NSTextAttachment设置的bounds是图片的大小,没有设置间隙的属性,所以就用这个空格去代替
网友评论