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

來源:互聯網
上載者:User

標籤:項目   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文本圖文混排)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.