In iOS development, there is often a piece of text that displays different colors and fonts, or the need to add lines or underscores to a few words. Before the online to find some information, some redraw Uilabel textlayer, some with HTML5 to achieve, are more troublesome, and many Uilabel properties also do not work, the effect is not ideal. Later I learned that nsmuttableattstring (string with attributes), some of the above requirements can be easily implemented.
- Instantiation methods and how to use them
Instantiation method:
Class with String.
1 -(ID) initwithstring: (NSString *) str;
Cases:
1 nsmutableattributedstring *attributedstr = [[nsmutableattributedstringalloc]initwithstring:@] It's a nice day today. " ]; 2 3 4 5 -(ID) initwithstring: (NSString *) str attributes: (nsdictionary *) attrs;
The dictionary holds some property names and property values, such as:
Nsdictionary *attributedict = [Nsdictionarydictionarywithobjectsandkeys: [uifontsystemfontofsize: 15.0 ],nsfontattributename, [Uicolorredcolor],nsforegroundcolorattributename, Nsunderlinestyleattributename,nsunderlinestylesingle,nil];
nsmutableattributedstring *attributedstr = [[nsmutableattributedstringalloc]initwithstring:@] The weather is good today "Attributes:attributedict" ;
-(ID) initwithattributedstring: (nsattributedstring *) Attester;
Use nsattributedstring initialization, similar to nsmutablestring,nsstring
How to use:
Set multiple properties for text in a range
-(void) SetAttributes: (nsdictionary *) Attrs range: (nsrange) range;
Add a property to a range of text
-(void) AddAttribute: (NSString *) Name value: (ID) value range: (nsrange) range;
Add multiple attributes to a range of text
-(void) AddAttributes: (nsdictionary *) Attrs range: (nsrange) range;
To remove an attribute in a range
-(void) RemoveAttribute: (NSString *) name range: (nsrange) range;
- Common Properties and descriptions
Nsfontattributename fonts
Nsparagraphstyleattributename paragraph format
Nsforegroundcolorattributename Font Color
Nsbackgroundcolorattributename background Color
Nsstrikethroughstyleattributename Strikethrough formatting
Nsunderlinestyleattributename underline format
Nsstrokecolorattributename Strikethrough Color
Nsstrokewidthattributename Strikethrough width
Nsshadowattributename Shadow
More methods and properties are described in the official Apple documentation:
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/ nsmutableattributedstring_class/reference/reference.html#//apple_ref/doc/uid/tp40003689
- Working with instances
1UILabel *testlabel = [[UILabel alloc]initwithframe:cgrectmake (0, -, the, -)];2 3Testlabel.backgroundcolor =[Uicolor Lightgraycolor];4 5Testlabel.textalignment =Nstextalignmentcenter;6 7nsmutableattributedstring *attributedstr = [[nsmutableattributedstring alloc]initwithstring:@"It's a nice day today."];8 9 [Attributedstr addattribute:nsfontattributenameTen OneValue:[uifont systemfontofsize:16.0] A -Range:nsmakerange (2,2)]; - the [Attributedstr addattribute:nsforegroundcolorattributename - - Value:[uicolor Redcolor] - +Range:nsmakerange (2,2)]; - +Testlabel.attributedtext =Attributedstr; A at[Self.view Addsubview:testlabel];
Operating effect:
In addition, other controls that can set text (such as Uibutton,uitextfield) also have this property, which is not detailed enough, but simply introduced, and other effects are implemented in the reference API for more properties and how to use it.
Reprinted from: http://snowyshell.blog.163.com/blog/static/2209140342014475383375/
iOS uses nsmutableattributedstring for rich text (different color fonts, underscores, etc.)