Nsmutableattributedstring IOS displays different fonts and colors in Uilabel (GO)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.