Generally use rich text to achieve a variety of label text, is the use of Uilabel classification quickly create colorful and diverse lable text, fast and simple, strong customization, more important is no code pollution
Https://github.com/joaoffcosta/UILabel-FormattedText
Add 2 new methods
* Custom add */-(void) SetFont: (uifont *) font substring: (nsstring*) substring; -(void) SetFont: (Uifont *) font Withtcolor: (Uicolor *) textcolor substring: (nsstring*) substring;
/** * custom add */-(void) SetFont: (uifont*) Font substring: (nsstring*) substring { Nsrange range = [Self.text Rangeofstring:substring]; if (range.location! = nsnotfound) { [self setfont:font range:range]; } -(void) SetFont: (Uifont *) font Withtcolor: (Uicolor *) textcolor substring: (nsstring*) substring{ Nsrange Range = [Self.text rangeofstring:substring]; if (range.location! = nsnotfound) { [self setfont:font range:range]; [Self Settextcolor:textcolor range:range];} }
Use
- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.Self.view.backgroundcolor=[Uicolor Blackcolor]; NSString*text=@" Guangdong think Port Group Direct holdings of new Sanbanxi listed companies lucky US shares, formally become quasi-listed companies. "; UILabel*labela =[[UILabel alloc] init]; [Labela Setframe:cgrectmake (0, the, Self.view.frame.size.width, -)]; [Labela Setbackgroundcolor:[uicolor Clearcolor]; [Labela Settextalignment:nstextalignmentcenter]; [Labela SetText:@"Color Label"]; [Labela Settextcolor:[uicolor Cyancolor]; [Labela setfont:[uifont systemfontofsize: A]];; [Labela Settextcolor:[uicolor Redcolor] String:@"Color"]; [Self.view Addsubview:labela]; //ColorsUILabel *LABELC =[[UILabel alloc] init]; [Labelc Setframe:cgrectmake (0, -, Self.view.frame.size.width, Floorf (Self.view.frame.size.height/5))]; [Labelc Setbackgroundcolor:[uicolor Clearcolor]; [Labelc Settextalignment:nstextalignmentcenter]; [Labelc Settext:text]; [LABELC setnumberoflines:2]; [Labelc Settextcolor:[uicolor Yellowcolor]; [Labelc setfont:[uifont Fontwithname:@"Courier"Size -]]; [Labelc Settextcolor:[uicolor Whitecolor] Range:nsmakerange ( -,3)]; [Labelc Settextcolor:[uicolor Cyancolor] Range:nsmakerange ( -,4)]; [Labelc Settextunderline:[uicolor Redcolor] Range:nsmakerange (Ten,6)]; [Self.view ADDSUBVIEW:LABELC]; //FontsUILabel *labelf =[[UILabel alloc] init]; [Labelf Setframe:cgrectmake (0, Floorf (Self.view.frame.size.height/4), Self.view.frame.size.width, Floorf (Self.view.frame.size.height/3))]; [Labelf Setbackgroundcolor:[uicolor Clearcolor]; [Labelf Settextalignment:nstextalignmentcenter]; [Labelf Settext:text]; [Labelf setnumberoflines:2]; [Labelf Settextcolor:[uicolor Yellowcolor]; [Labelf setfont:[uifont Fontwithname:@"Courier"Size -]]; [Labelf Settextcolor:[uicolor Redcolor] String:@"Lucky US shares"]; [Labelf setfont:[uifont systemfontofsize: -] Withtcolor:[uicolor Whitecolor] substring:@"si bu group"]; [Self.view addsubview:labelf]; //Colors and FontsUILabel *LABELCF =[[UILabel alloc] init]; [LABELCF Setframe:cgrectmake (0, Floorf (Self.view.frame.size.height *2/4), Self.view.frame.size.width, Floorf (Self.view.frame.size.height/3))]; [LABELCF Setbackgroundcolor:[uicolor Clearcolor]; [LABELCF Settextalignment:nstextalignmentcenter]; [LABELCF Settext:text]; [LABELCF setnumberoflines:2]; [LABELCF Settextcolor:[uicolor Yellowcolor]; [LABELCF setfont:[uifont Fontwithname:@"Courier"Size -]]; [LABELCF Settextcolor:[uicolor Redcolor] Range:nsmakerange ( -,8)]; [LABELCF setfont:[uifont Fontwithname:@"Courier-bold"Size -] Range:nsmakerange ( -,8)]; [LABELCF Settextcolor:[uicolor Cyancolor] Range:nsmakerange ( -, One)]; [LABELCF setfont:[uifont Fontwithname:@"Courier-oblique"Size -] Range:nsmakerange ( -, One)]; [Self.view ADDSUBVIEW:LABELCF];}
Demo:http://files.cnblogs.com/files/sixindev/uilabelformattedtext.zip
Create colorful text label by HL