Nsattributedstring use method to organize

Source: Internet
Author: User
Tags control label transparent color

Nsattributedstring, is a string with attributes (Rich text), divided into nsattributedstring and nsmutableattributedstring. Can be used in UILabel, Uitextview and other places.

property is a dictionary with the property name key. The Common properties are:

1     //nsfontattributename Setting font properties, default: Font: Helvetica (Neue) font size:2     //Nsforegroundcolorattributenam Set Font color, value to Uicolor object, default value is black3     //Nsbackgroundcolorattributename Set the region background color of the font, the value is Uicolor object, the default value is nil, transparent color4     //Nsligatureattributename Sets the conjoined property, with a value of NSNumber object (integer), 0 for no conjoined character, and 1 for the default conjoined character5     //nskernattributename Set character spacing, value is NSNumber object (integer), positive spacing is widened, and negative spacing narrows6     //Nsstrikethroughstyleattributename set Strikethrough, value is NSNumber object (integer)7     //nsstrikethroughcolorattributename Set Strikethrough color, value is Uicolor object, default value is black8     //Nsunderlinestyleattributename sets an underscore to a value of NSNumber object (integer), enumeration of values in constant Nsunderlinestyle, similar to strikethrough9     //Nsunderlinecolorattributename Sets the underline color, the value is the Uicolor object, the default value is blackTen     //Nsstrokewidthattributename Sets the stroke width, takes a value of NSNumber object (integer), negative fill effect, positive hollow effect One     //Nsstrokecolorattributename fills part of the color, not the font color, and takes the value of the Uicolor object A     //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 the     //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 the link properties, click on the browser 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

How to use:

1. Initialize a nsmutableattributedstring, add a property to it, and assign it to the attributedtext of the control, for cases where the text is less and requires fine control.

1NSString *originstr =@"testing Rich Text Properties";2     //Create nsmutableattributedstring3Nsmutableattributedstring *attributedstr =[[Nsmutableattributedstring alloc] initwithstring:originstr];4     5     //Add Property6     //set all fonts to bold # 14th7[Attributedstr addattribute:nsfontattributename value: [Uifont boldsystemfontofsize: -]8Range:nsmakerange (0, Originstr.length)];9     //set the first 4 fonts to blueTen[Attributedstr addattribute:nsforegroundcolorattributename value: [Uicolor Bluecolor] Range:nsmakerange (0,4)]; One      A     //assign a value to the attributedtext of the Display control label -_label.attributedtext = Attributedstr;

2. Create an attribute dictionary to initialize the properties. It is then created and assigned to the control's attributedtext along with the text that needs to be controlled, which is suitable for situations where there is more overall control over the text that needs to be controlled, usually a large piece of text that is read from the file.

1NSString *originstr =@"testing Rich Text Properties";2     //Create a property dictionary3Nsdictionary *attrdict = @{nsfontattributename: [Uifont systemfontofsize: the],4 nsforegroundcolorattributename: [Uicolor Bluecolor]};5     //assigns a string and property to the control's attributedtext, or you can specify a range6Label.attributedtext = [[Nsattributedstring alloc] Initwithstring:[originstr substringwithrange:nsmakerange (0,6)] [attributes:attrdict];

The use of the explanation:

The color set by the 1.NSForegroundColorAttributeName setting is equal to the Uilabel's TextColor property, and who is the last to be assigned, and whose color is ultimately displayed. However, the TextColor property can be overlaid with the Nsbackgroundcolorattributename property.

2. Nsstrikethroughstyleattributename to set the value in the Nsunderlinestyle

1 // Nsunderlinestylenone   do not set strikethrough 2//  Nsunderlinestylesingle set strikethrough to thin solid line 3  //  Nsunderlinestylethick  set strikethrough to coarse solid line 4//  Nsunderlinestyledouble set strikethrough to thin double solid line
These enumeration constants are NSNumber that contain an integer, so you must first convert to NSNumber to use the same enumeration constants as their property values using strikethrough and underscores.
1 nsdictionary *attrdict = @{nsstrikethroughstyleattributename: @ (nsunderlinestylesingle),2                               nsfontattributename: [Uifont systemfontofsize:]}; 3 _label.attributedtext = [[Nsattributedstring alloc] Initwithstring:originstr attributes:attrdict];
The location of the strikethrough in Chinese and English is different. In addition, the strikethrough attribute value in addition to the above 4 kinds of, in fact, you can also take other integer values: When the value is 0-7, the effect is solid line, with the value of increasing, the single solid line gradually become coarse, take 9-15, the effect is double solid line, the larger the value, the more coarse the double solid line.
1 nsdictionary *attrdict = @{nsstrikethroughstyleattributename: @ (1),2                               Nsfontattributename: [Uifont systemfontofsize:]}; 3 _label.attributedtext = [[Nsattributedstring alloc] Initwithstring:originstr attributes:attrdict];

3. Underline and strikethrough are similar, you can refer to the settings

1Nsunderlinestylenone//No underline2Nsunderlinestylesingle//Single Underline3Nsunderlinestylethick//Bold Underline4Nsunderlinestyledouble//Double Underline5Nsunderlinepatternsolid//Solid Underline6Nsunderlinepatterndot//Dot Underline7Nsunderlinepatterndash//Dash underline8Nsunderlinepatterndashdot//dash and Dot underline9Nsunderlinepatterndashdotdot//a dash and an underscore of two dotsTenNsunderlinebyword//underline text

4.NSParagraphStyleAttributeName set paragraph styles, including:

1 //alignment alignment;2 //firstlineheadindent: First line indent3 //Tailindent: Paragraph trailing indent4 //headindent: Indent in the front of paragraph (indent left) )5 //maximumlineheight: Maximum row height6 //minimumlineheight: Minimum row height7 //linespacing: Line spacing8 //paragraphspacing: Paragraph spacing9 //Linebreakmode: Break modeTen //defaultwritingdirectionforlanguage: Default direction of writing (you can specify a language) One //Basewritingdirection (Basic writing direction)

There are 6 types of Linebreakmode:

 1  //  2  //  3  //  4  //  5  //  6  // 

Nsattributedstring use method to organize

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.