- 图文换牌主要就是用NSTextAttachment,来添加附件。NSAttributedString来添加属性字符串
let strRed = NSAttributedString(string: "这是我的", attributes: [NSAttributedString.Key.foregroundColor: UIColor.red])
let strBlue = NSAttributedString(string: "属性字符串", attributes: [NSAttributedString.Key.foregroundColor: UIColor.blue])
//创建图片
let attacment = NSTextAttachment()
attacment.image = UIImage(named:"imageName")
let font = attriLabel.font
attacment.bounds = CGRect(x: 0, y: -4, width: font!.lineHeight , height: font!.lineHeight)
let attacStr = NSAttributedString(attachment: attacment)
//拼接字符串
let mutableStr = NSMutableAttributedString()
mutableStr.append(strRed)
mutableStr.append(strBlue)
mutableStr.append(attacStr)
attriLabel.attributedText = mutableStr
网友评论