iOS开发笔记-143: swift5-富文本图文混排

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
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容