美文网首页
Swift 图文混排 富文本

Swift 图文混排 富文本

作者: 越天高 | 来源:发表于2020-04-29 14:42 被阅读0次
  • 图文换牌主要就是用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

相关文章

网友评论

      本文标题:Swift 图文混排 富文本

      本文链接:https://www.haomeiwen.com/subject/faggwhtx.html