iOS uses nsmutableattributedstring for rich text (different color fonts, underscores, etc.)

Source: Internet
Author: User

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.

    1. 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;

    1. 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

    1. 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.)

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.