文本中添加icon图标(图文混排)

UI展示:


WechatIMG130.jpeg

代码:

let attString = NSMutableAttributedString()
let font = UIFont.boldSystemFont(ofSize: 16.0)
let leftAtt = NSAttributedString(string: "搜索", attributes: [.font:font])
let imgAttach = NSTextAttachment()
if let img = UIImage(named: "ckjg.png") {
      imgAttach.image = img
      imgAttach.bounds = .init(x: 0, y: (img.size.height - font.lineHeight) / 2, width: img.size.width, height: img.size.height)
}
let imgAtt = NSMutableAttributedString(attachment: imgAttach)
let rightAtt = NSAttributedString(string: "查看结果", attributes: [.font:font])
attString.append(leftAtt)
attString.append(imgAtt)
attString.append(rightAtt)
label.attributedText = attString

你可能感兴趣的:(文本中添加icon图标(图文混排))