iOS set line spacing and partial text color

Source: Internet
Author: User

/** * Set line spacing and word spacing * * @param linespace line spacing * @param kern word spacing * * @return Rich Text*/-(Nsmutableattributedstring *) Getattributedstringwithlinespace: (NSString *) Text linespace: (cgfloat) Linespace Kern: (cgfloat) Kern {Nsmutableparagraphstyle* Paragraphstyle = [NsmutableparagraphstyleNew]; //Adjust line spacingparagraphstyle.linespacing=Linespace; Paragraphstyle.alignment=Nstextalignmentleft; Paragraphstyle.linespacing= Linespace;//Set Line spacingParagraphstyle.firstlineheadindent =30.0;//set the first line indentNsdictionary*attridict =@{nsparagraphstyleattributename:paragraphstyle,nskernattributename:@ (Kern)}; Nsmutableattributedstring* Attributedstring =[[Nsmutableattributedstring alloc] Initwithstring:text attributes:attridict]; returnattributedstring;}/** * Rich Text part Font set COLOR * * @param text text * @param highlighttext Set the color of * * * @return Rich Text*/-(Nsmutableattributedstring *) setupattributestring: (NSString *) text highlighttext: (NSString *) Highlighttext {nsrange Hightlighttextrange=[text Rangeofstring:highlighttext]; Nsmutableattributedstring*attributestr =[[Nsmutableattributedstring alloc] initwithstring:text]; if(Hightlighttextrange.length >0) {[Attributestr addattribute:nsforegroundcolorattributename VALUE:[HBPLISTRESOURC Eutil Colorwithname:@"Maincolor"] Range:hightlighttextrange]; [Attributestr addattribute:nsfontattributename value:[uifont boldsystemfontofsize:15.0f] Range:hightlighttextrange]; returnAttributestr; }Else {        return[highlighttext copy]; }}

The combination of the two:

/** * Set line spacing, word spacing, and text color * * @param linespace line spacing * @param kern word spacing * @param colortext Set Color text * * @return Rich Text*/-(Nsmutableattributedstring *) Getattributedstringwithlinespace: (NSString *) text linespace: (cgfloat) LineSpace Kern :(cgfloat) Kern Colortext: (NSString *) colortext{Nsmutableparagraphstyle* Paragraphstyle = [NsmutableparagraphstyleNew]; //Adjust line spacingparagraphstyle.linespacing=Linespace; Paragraphstyle.alignment=Nstextalignmentleft; Paragraphstyle.linespacing= Linespace;//Set Line spacingParagraphstyle.firstlineheadindent =30.0;//set the first line indentNsdictionary*attridict =@{nsparagraphstyleattributename:paragraphstyle,nskernattributename:@ (Kern)}; Nsrange Colortextrange=[text Rangeofstring:colortext]; Nsmutableattributedstring*attributestr =[[Nsmutableattributedstring alloc] initwithstring:text]; //Set Text color[attributestr addattribute:nsforegroundcolorattributename value:[hbplistresourceutil Color Withname:@"Maincolor"] Range:colortextrange]; [Attributestr addattribute:nsfontattributename value:[uifont boldsystemfontofsize:15.0f] Range:colortextrange]; [Attributestr addattributes:attridict Range:nsmakerange (0, [text length]); returnattributestr;}

iOS set line spacing and partial text color

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.