Swift 图文混排 富文本

2020-04-29  本文已影响0人  越天高

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
上一篇 下一篇

猜你喜欢

热点阅读