Use nsattributedstring to create labels flexibly in iOS

Source: Internet
Author: User

Flexible using nsattributedstring makes it easier to create labels that are complex in content. One thing to note: If a label sets this property, its other settings will be invalidated.

First, we initialize a Nsmutableattributedstring object.

Initialize//-(instancetype) initwithstring with string initialization: (NSString *) str;//Direct initialization of//-(Instancetype) initwithstring via string and attribute dictionaries: ( NSString *) str attributes: (nsdictionary *) attrs;//initialize//-(instancetype) initwithattributedstring via its own object: ( Nsattributedstring *) Attrstr; nsmutableattributedstring * attribute = [[nsmutableattributedstring alloc]initwithstring:@] [email protected] #你好么QWE " ];

The Attrebute string can be set and modified by the following two functions

can replace character-(void) Replacecharactersinrange: (nsrange) Range withstring: (NSString *) str;//property setting-(void) SetAttributes: ( Nsdictionary *) Attrs range: (nsrange) range;//set certain range of character properties-(void) AddAttribute: (NSString *) Name value: (ID) value range: ( Nsrange) range;

The dictionary key values correspond to the following:

kctfontattributename  This key is the name of the font   must pass in the Ctfont object//kctkernattributename  this key to set the font spacing   Incoming must be a numeric object   Default is 0//kctligatureattributename   this key to set hyphenation   must pass in Cfnumber object//kctparagraphstyleattributename    paragraph to its way//kctforegroundcolorattributename  font color   must pass in Cgcolor object//kctstrokewidthattributename   Stroke width   must be a Cfnumber object//kctstrokecolorattributename  stroke Color//kctsuperscriptattributename  Controls vertical text positioning  cfnumber Object//kctunderlinecolorattributename  underline color [Attribute addattribute: (NSString*) Kctkernattributename value:@5 range:nsmakerange (0, 5)]; [Attribute addattribute: (nsstring *) kctfontattributename                         value: ( ID) cfbridgingrelease (ctfontcreatewithname (cfstringref) [uifont boldsystemfontofsize:14].fontname,                                                          14,                                                          null))                          range:nsmakerange (0,  4)];    [attribute addattribute: (nsstring *) KCTUnderlineStyleAttributeName                        &Nbsp; value: (ID) [nsnumber numberwithint:kctunderlinestyledouble]                          Range:nsmakerange (0, 4)];

Through the test, found that some of the above key values do not work, you can replace the following method, the effect is the same, the difference is that the value of the different types, the following method is more convenient (using Uifont uicolor NSString and some system enumeration)

Nsparagraphstyleattributenamensforegroundcolorattributenamensbackgroundcolorattributenamensligatureattributenamenskernatt Ributenamensstrikethroughstyleattributenamensunderlinestyleattributenamensstrokecolorattributename Nsstrokewidthattributename Nsshadowattributename Nstexteffectattributenamensattachmentattributename Nslinkattributename Nsbaselineoffsetattributename Nsunderlinecolorattributenamensstrikethroughcolorattributenamensobliquenessattributename Nsexpansionattributename Nswritingdirectionattributenamensverticalglyphformattributename


Use nsattributedstring to create labels flexibly in iOS

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.