iOS-Swift3富文本(UILable文本图文混排)

转载注明出处:http://blog.csdn.net/qxuewei/article/details/53213636
介绍下iOS开发中常用的符文布图文混排
需求: 邱学伟是大帅哥(加个笑脸图片) 邱学伟:红色背景绿色字体加粗显示 是:蓝色字体 10号小字体 大帅哥:灰色42号字体
UILabel中显示结果:
这里写图片描述

原谅我跟哥们开玩笑起的low爆了的项目名
核心代码:

//需求 邱学伟是大帅哥(加个笑脸图片)   邱学伟:红色背景绿色字体加粗显示   是:蓝色字体 10号小字体  大帅哥:灰色42号字体
    fileprivate func FuWenBenDemo() {

        //定义富文本即有格式的字符串
        let attributedStrM : NSMutableAttributedString = NSMutableAttributedString()

        //邱学伟
        let qiuxuewei : NSAttributedString = NSAttributedString(string: "邱学伟", attributes: [ NSBackgroundColorAttributeName : UIColor.red,NSForegroundColorAttributeName : UIColor.green, NSFontAttributeName : UIFont.boldSystemFont(ofSize: 28.0)]) //(string: "邱学伟")
        //是
        let shi : NSAttributedString = NSAttributedString(string: "是", attributes: [NSForegroundColorAttributeName : UIColor.blue, NSFontAttributeName : UIFont.systemFont(ofSize: 10.0)])
        //大帅哥
        let dashuaige : NSAttributedString = NSAttributedString(string: "大帅哥", attributes: [NSForegroundColorAttributeName : UIColor.lightGray, NSFontAttributeName : UIFont.systemFont(ofSize: 42.0)])
        //笑脸图片
        let smileImage : UIImage = UIImage(named: "d_hehe")!
        let textAttachment : NSTextAttachment = NSTextAttachment()
        textAttachment.image = smileImage
        textAttachment.bounds = CGRect(x: 0, y: -4, width: 22, height: 22)

        attributedStrM.append(qiuxuewei)
        attributedStrM.append(shi)
        attributedStrM.append(dashuaige)
        attributedStrM.append(NSAttributedString(attachment: textAttachment))

        label.attributedText = attributedStrM
    }

完整项目链接:https://git.oschina.net/qxuewei/AttributedStringDemo

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值