let nameAstr = NSMutableAttributedString.init(string: " 发布时间: 11111")
nameAstr.addAttributes([NSAttributedString.Key.foregroundColor : kMainColor99], range: NSRange(location: 0, length: nameAstr.length))
nameAstr.addAttributes([NSAttributedString.Key.font : FONT12], range: NSRange(location: 0, length: nameAstr.length))
if let image = R.image.home_341() {
let imageH: CGFloat = 14//显示的图片宽高
let imageW: CGFloat = 14
let font = UIFont.systemFont(ofSize: 12)
let textAttachment = NSTextAttachment()
textAttachment.image = image
textAttachment.bounds = CGRect(x: 0, y: (font.capHeight - imageH).rounded()/2, width: imageW, height: imageH).integral
let attachmentStr = NSAttributedString(attachment: textAttachment)
nameAstr.insert(attachmentStr, at: 0)//图片放在第0位置
createdate.attributedText = nameAstr
}
iOS开发笔记-143: swift5-富文本图文混排
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 说明:旨在减少对第三方的依赖,一个比较简单的实现思路;比起像YYLabel等强大的框架只是冰山一角!重在学习☺️☺...
- 设置富文本所有的key(共21种) NSMutableAttributedString*abs = [[NSMut...
- 最近项目中对图文混排有一定的需求,例如价格,文字链接,文字颜色变化等要求,翻了很多资料,咱们对这些属性做了如下的总...