swift 版 富文本图文混排

2021-03-16  本文已影响0人  授之以渔不如授之以鱼
image.png

大家都是成年人挺忙的,码来!

lazy var specialUITextView: YYTextView = {
        let textView = YYTextView()
        textView.isEditable = false
        textView.isScrollEnabled = false
//        textView.textAlignment = .center
        //创建富文本
        let text = NSMutableAttributedString.init(string: "*由支付宝提供身份认证功能")
        text.yy_font = .systemFont(ofSize: 14)
        text.yy_color = ColorGray5
        text.yy_setColor(.red, range: NSRange(location: 0,length: 1))
        text.yy_alignment = .center
        // 将图片附件转为字符串
        let image = UIImage(named: "icon_zhifubao")
        var attachment: NSMutableAttributedString? = nil
        attachment = NSMutableAttributedString.yy_attachmentString(withContent: image, contentMode: UIView.ContentMode.center, attachmentSize: CGSize(width: 14, height: 14), alignTo: .systemFont(ofSize: 14), alignment: .center)
        //插入图片附件到富文本
        if let attachment = attachment {
            text.insert(attachment, at: 2)
        }
        textView.attributedText = text
        return textView
    }()
上一篇 下一篇

猜你喜欢

热点阅读