美文网首页
图文混排

图文混排

作者: CoderLWG | 来源:发表于2018-01-17 14:04 被阅读8次

    直接看代码

    let attrStr = NSAttributedString(string: "我不知道", attributes: [NSForegroundColorAttributeName : UIColor.red])
            let attrStr1 = NSAttributedString(string: "你是谁", attributes: [NSForegroundColorAttributeName : UIColor.blue])
            
            // 图文混排
            let attachment = NSTextAttachment()
            attachment.image = UIImage(named: "d_aini")
            let font = lab.font
            attachment.bounds = CGRect(x: 0, y: -3, width: (font?.lineHeight)!, height: (font?.lineHeight)!)
            
            let attrImageStr = NSAttributedString(attachment: attachment)
            
            
            let attrMStr = NSMutableAttributedString()
            attrMStr.append(attrStr)
            attrMStr.append(attrImageStr)
            attrMStr.append(attrStr1)
            
            lab.attributedText = attrMStr
    
    生成效果如下 image.png

    相关文章

      网友评论

          本文标题:图文混排

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