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