Reprint Annotated Source: http://blog.csdn.net/qxuewei/article/details/53213636
Introduce the common Runecloth and graphics in iOS development
Requirements: Chu Cowei is big handsome (add a smiley picture) Chu Cowei: Red background Green font bold display is: Blue font 10th small font: Gray 42nd Font
Results shown in Uilabel:
Forgive me for the low-blown project name that I joked with my buddies.
Core code:
//Demand Chu Cowei is big handsome (add a smiley picture) Chu Cowei: Red background Green font bold display is: Blue font 10th small font handsome: Gray 42nd FontFileprivate func Fuwenbendemo () {//define Rich text as a formatted stringLet attributedstrm:nsmutableattributedstring = nsmutableattributedstring ()//Chu CoweiLet qiuxuewei:nsattributedstring = nsattributedstring (string:"Chu Cowei", Attributes: [Nsbackgroundcolorattributename:Uicolor. Red, Nsforegroundcolorattributename:Uicolor. Green, Nsfontattributename:Uifont. Boldsystemfont(Ofsize:28.0)])//(string: "Chu Cowei") //YesLet shi:nsattributedstring = nsattributedstring (string:"Yes", Attributes: [Nsforegroundcolorattributename:Uicolor. Blue, Nsfontattributename:Uifont. Systemfont(Ofsize:10.0)])///StudLet dashuaige:nsattributedstring = nsattributedstring (string:"Handsome man.", Attributes: [Nsforegroundcolorattributename:Uicolor. Lightgray, Nsfontattributename:Uifont. Systemfont(Ofsize:42.0)])//Smiley pictureLet Smileimage:UIImage=UIImage(named:"D_hehe")! Let textattachment:nstextattachment = Nstextattachment () textattachment. Image= Smileimage Textattachment. Bounds=CGRect(x:0, Y:-4, Width: A, Height: A) ATTRIBUTEDSTRM. Append(Qiuxuewei) ATTRIBUTEDSTRM. Append(shi) attributedstrm. Append(Dashuaige) ATTRIBUTEDSTRM. Append(Nsattributedstring (attachment:textattachment)) label. Attributedtext= ATTRIBUTEDSTRM}
Complete Project Link: Https://git.oschina.net/qxuewei/AttributedStringDemo
Ios-swift3 Rich Text (uilable text graphics mixed)