標籤:項目 hid 代碼 mutable down markdown chm 灰色 osc
轉載註明出處: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
iOS-Swift3富文本(UILable文本圖文混排)