In project development, we often encounter situations in which a uilabel uses different colors or different fonts to embody a string in an iOS6In the future we can easily achieve this, the official API provides us with the Uilabel class of attributedtext, using different colors and different font strings, we can use Nsattributedtext and Nsmutableattributedtext class to implement. Real code:@interfaceViewcontroller:uiviewcontroller@property (nonatomic, strong) Iboutlet UILabel*Attrlabel;-(Ibaction) Next: (ID) sender;@endAdd the following code to the. m file in the Viewdidload method:Self.title=@"For IOS 6 & later"; Nsmutableattributedstring*STR = [[Nsmutableattributedstring alloc] initwithstring:@"Using nsattributed String"]; [Str addattribute:nsforegroundcolorattributename value:[uicolor Bluecolor] Range:nsmakerange (0,5)]; [Str addattribute:nsforegroundcolorattributename value:[uicolor Redcolor] Range:nsmakerange (6, A)]; [Str addattribute:nsforegroundcolorattributename value:[uicolor Greencolor] Range:nsmakerange ( +,6)]; [Str addattribute:nsfontattributename value:[uifont fontwithname:@"ARIAL-BOLDITALICMT"Size30.0] Range:nsmakerange (0,5)]; [Str addattribute:nsfontattributename value:[uifont fontwithname:@"Helveticaneue-bold"Size30.0] Range:nsmakerange (6, A)]; [Str addattribute:nsfontattributename value:[uifont fontwithname:@"Courier-boldoblique"Size30.0] Range:nsmakerange ( +,6)];attrlabel.attributedtext= str;
NSString *ConstNsforegroundcolorattributename;//The value is Uicolor, the font color, and the default is black. NSString*ConstNsbackgroundcolorattributename;//The value is Uicolor, the font background color, not by default. NSString*ConstNsligatureattributename;//The value is integer type NSNumber, hyphenation attribute, general Chinese can not be used, in English may appear adjacent letter pen case. 0 is not a pen, 1 is the default pen, and the default value, 2 is not supported on iOS. NSString*ConstNskernattributename;//The value is floating-point number NSNumber, the Kerning property, and the default value is 0. NSString*ConstNsstrikethroughstyleattributename;//The value is an integral type NSNumber, and the value isenum{Nsunderlinestylenone=0Xxx, Nsunderlinestylesingle=0X on,}; Set strikethrough. NSString*ConstNsunderlinestyleattributename;//Ibid. Sets the underline. NSString*ConstNsstrokecolorattributename;//The value is Uicolor, the default value is nil, and the property set is the same as Foregroundcolor. NSString*ConstNsstrokewidthattributename;//The value is a floating-point number nsnumber. Sets the thickness of the drawing. NSString*ConstNsshadowattributename;//a value of Nsshadow sets the shadow of the picture, and the default value is nil. NSString*ConstNsverticalglyphformattributename;//The value is integer nsnumber,0 is the horizontal layout of the word, 1 is the vertical layout of the word.
Nsmutableattributedstring IOS displays different fonts and colors in Uilabel (GO)