Simple use of nsmutableattributedstring

Source: Internet
Author: User

1,

// Running Effect

 

Self. attributestringlabel. attributedtext = [self getattributedstringwithstring: @ "Fu is the font of this attribute" astring: @ "color" astring: @ "underline"];

 

 

/**

* Nsmutableattributedstring simple demo Method

*

* @ Param str1 the first property string

* @ Param str2 the first property string

* @ Param str3 the first attribute string

*

* @ Return nsmutableattributedstring

*/

-(Nsmutableattributedstring *) getattributedstringwithstring :( nsstring *) str1 astring :( nsstring *) str2 astring :( nsstring *) str3 {

Nsstring * normalstring = [nsstring stringwithformat: @ "% @ -- % @", str1, str2, str3];

Nsmutableattributedstring * attredstring = [[nsmutableattributedstring alloc] initwithstring: normalstring] autorelease];

// The nsange of each attribute

Nsange range1 = [normalstring rangeofstring: str1];

Nsange range2 = [normalstring rangeofstring: str2];

Nsange range3 = [normalstring rangeofstring: str3];

// Specific attributes in each nsange (all attributes can be queried by nsattributedstring in the uikit Framework)

Nsdictionary * attres1 = [nsdictionary dictionarywithobject: [uifont fontwithname: @ "palatino-Roman" Size: 20366f] forkey: nsfontattributename];

Nsdictionary * attres2 = [nsdictionary dictionarywithobject: [uicolor purplecolor] forkey: nsforegroundcolorattributename];

Nsdictionary * attres3 = [nsdictionary dictionarywithobject: [uicolor redcolor] forkey: nsunderlinecolorattributename];

Nsdictionary * attres4 = [nsdictionary dictionarywithobject: [nsstring stringwithformat: @ "1"] forkey: nsunderlinestyleattributename];

// Set attributes

[Attredstring addattributes: attres1 range: range1];

[Attredstring addattributes: attres2 range: range2];

[Attredstring addattributes: attres3 range: range3];

[Attredstring addattributes: attres4 range: range3];

Return attredstring;

}

 

 

 

2. After ios7, A New Rich Text API can be added through URL and stream

Optional * attredstring1 = [[nsmutableattributedstring alloc] initwithfileurl: <# (nsurl *) #> options: <# (nsdictionary *) #> documentattributes: <# (nsdictionary **) #> error: <# (nserror **) #>]

 

Optional * attredstring2 = [nsmutableattributedstring alloc] initwithdata: <# (nsdata *) #> options: <# (nsdictionary *) #> documentattributes: <# (nsdictionary **) #> error: <# (nserror **) #>;

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.