直接看代码
let attrStr = NSAttributedString(string: "我不知道", attributes: [NSForegroundColorAttributeName : UIColor.red])
let attrStr1 = NSAttributedString(string: "你是谁", attributes: [NSForegroundColorAttributeName : UIColor.blue])
// 图文混排
let attachment = NSTextAttachment()
attachment.image = UIImage(named: "d_aini")
let font = lab.font
attachment.bounds = CGRect(x: 0, y: -3, width: (font?.lineHeight)!, height: (font?.lineHeight)!)
let attrImageStr = NSAttributedString(attachment: attachment)
let attrMStr = NSMutableAttributedString()
attrMStr.append(attrStr)
attrMStr.append(attrImageStr)
attrMStr.append(attrStr1)
lab.attributedText = attrMStr
生成效果如下
image.png
网友评论