Easy use of iOS Rich text attributedstring

Source: Internet
Author: User

Rich Text, as the name implies is Rich Text format, this article demo use nsmutableattributedstring

Get Rich Text
Nsmutableattributedstring*attributestring_atts=[[nsmutableattributedstring alloc]initwithstring:string];
Background color
Uicolor *backgroundcolor=[uicolor Whitecolor];
Font
Uifont *font=[uifont fontwithname:@ "Stheitisc-light" size:18.0];
Offset amount
NSNumber *baselineoffset=[nsnumber numberwithfloat:10.0];
Text color
Uicolor *foregroundcolor=[uicolor Blackcolor];
Word spacing
NSNumber *kern=[nsnumber numberwithfloat:1.5];
Conjoined word
NSNumber *ligature=[nsnumber numberwithfloat:0];
Hyperlink text
Nsurl *linkurl=[nsurl urlwithstring:@ "www.baidu.com"];
Underline format
NSNumber *underline=[nsnumber Numberwithint:nsunderlinestylesingle];
Paragraph format paragraphs have more paragraphs in the format
Nsmutableparagraphstyle *paragraphstyle=[[nsmutableparagraphstyle Alloc]init];
Line spacing
[Paragraphstyle setlinespacing:10.0];
New Step Indent
[Paragraphstyle setheadindent:10.0];
First Line Indent
[Paragraphstyle setfirstlineheadindent:21.0];
Break by default by word
paragraphstyle.linebreakmode=nslinebreakbywordwrapping;
//
[Paragraphstyle settailindent:375.0];
Nsdictionary *[email protected]{
Nsforegroundcolorattributename:foregroundcolor,
Nsbackgroundcolorattributename:backgroundcolor,
Nsbaselineoffsetattributename:baselineoffset,
Nsfontattributename:font,
Nskernattributename:kern,
Nsligatureattributename:ligature,
Nslinkattributename:linkurl,
Nsunderlinestyleattributename:underline,
Nsparagraphstyleattributename:paragraphstyle
};

Full-text usage
[Attributestring_atts Addattributes:attrsdic range:nsmakerange (0, [string length])];

Calculating the width of rich text
CGRect Attrsrect=[attributestring_atts Boundingrectwithsize:cgsizemake (self.view.frame.size.width-20, CGFLOAT_MAX ) options:nsstringdrawinguseslinefragmentorigin| Nsstringdrawingusesfontleading Context:nil];
NSLog (@ "%f", attrsRect.size.width);
NSLog (@ "%f", attrsRect.size.height);
Uilabel*label=[[uilabel Alloc]initwithframe:cgrectmake (Ten, 200*number, self.view.frame.size.width-20, AttrsRect.size.height)];
Label.attributedtext=attributestring_atts;
Label.userinteractionenabled=yes;
label.numberoflines=0;
[Self.view Addsubview:label];

Attributedstring the way to create, the following study under the attributedstring can be set which properties, specifically, there are the following 21:

Nsfontattributename setting font properties, default: Font: Helvetica (Neue) font Size: 12
Nsforegroundcolorattributenam set font color, value to Uicolor object, default value is black
Nsbackgroundcolorattributename set the region background color of the font, the value is Uicolor object, the default value is nil, transparent color
Nsligatureattributename sets the conjoined property, with a value of NSNumber object (integer), 0 for no conjoined character, and 1 for the default conjoined character
Nskernattributename set character spacing, value is NSNumber object (integer), positive spacing is widened, and negative spacing narrows
Nsstrikethroughstyleattributename set Strikethrough, value is NSNumber object (integer)
Nsstrikethroughcolorattributename set strikethrough color, value is Uicolor object, default value is black
Nsunderlinestyleattributename sets an underscore to a value of NSNumber object (integer), enumeration of values in constant Nsunderlinestyle, similar to strikethrough
Nsunderlinecolorattributename sets the underline color, the value is the Uicolor object, the default value is black
Nsstrokewidthattributename Sets the stroke width, takes a value of NSNumber object (integer), negative fill effect, positive hollow effect
Nsstrokecolorattributename fills part of the color, not the font color, and takes the value of the Uicolor object
Nsshadowattributename Set the Shadow property to a value of Nsshadow object
Nstexteffectattributename sets the text special effect, the value is the NSString object, currently only the plate printing effect is available:
Nsbaselineoffsetattributename set the baseline offset value to NSNumber (float), positive offset, negative bias
Nsobliquenessattributename set the Glyph tilt, value NSNumber (float), positive right, negative left
Nsexpansionattributename set text stretch properties, value nsnumber (float), positive values stretch text horizontally, negative values compress text horizontally
Nswritingdirectionattributename setting the direction of writing, writing from left to right or from right to left
Nsverticalglyphformattributename sets the text layout direction, takes a value of NSNumber object (integer), 0 for horizontal text, and 1 for vertical text
Nslinkattributename Set link Properties, click to open the specified URL address
Nsattachmentattributename Set the text attachment, the value is the Nstextattachment object, commonly used in the text picture mixed row
Nsparagraphstyleattributename set the text paragraph layout format, the value is Nsparagraphstyle object

Paragraph
Alignment//Alignment
Firstlineheadindent//First line Indent
Headindent//Indent
Tailindent//Trailing indent
Linebreakmode//Break method
Maximumlineheight//MAX row height
Minimumlineheight//Minimum row height
LineSpacing//Line spacing
Paragraphspacing//Segment Distance
Paragraphspacingbefore//Segment head space
Basewritingdirection//sentence direction
Lineheightmultiple//variable row height, multiply factor.
Hyphenationfactor//Hyphen Property

Easy use of iOS Rich text attributedstring

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.