iOS learning Nsattributedstring (Rich Text)

Source: Internet
Author: User

nsattributedstring is called Rich Text , is a string with attributes, it can easily in a string to show a variety of fonts, size, font size and other different styles, but also can to format a paragraph, it is generally done with variable Rich text (nsmutableattributedstring)

First,some common methods of nsmutableattributedstring class
//Add a single text attribute to a range//Parameter 1: Character property name//Parameter 2: Property value//Parameter 3: Range- (void) AddAttribute: (NSString *) Name value: (ID) Value range: (nsrange) range;//add multiple text attributes in a range using a dictionary//parameter 1: Dictionary of Properties//Parameter 2: Range- (void) AddAttributes: (Nsdictionary<nsstring *,ID> *) Attrs Range: (nsrange) range;//Delete a text attribute with text in a range//Parameter 1: Character property name//Parameter 2: Range- (void) RemoveAttribute: (NSString *) name range: (nsrange) range;//Replace a string in a certain range//parameter 1: Range//Parameter 2: The string to replace- (void) Replacecharactersinrange: (nsrange) Range withattributedstring: (nsattributedstring *) attrstring;//Insert Rich Text at the corresponding corner mark//parameter 1: The string to insert//parameter 2: The position of the corner label to insert- (void) Insertattributedstring: (nsattributedstring *) attrstring Atindex: (nsuinteger) loc;//stitching a rich text to the back//parameters: The string to be spliced- (void) Appendattributedstring: (nsattributedstring *) attrstring;//remove characters from a range//Parameters: Range- (void) Deletecharactersinrange: (nsrange) range;// Replace all strings with another rich-text string//parameters: A rich text string after displacement- (void) Setattributedstring: (nsattributedstring *) attrstring;
Second, character Properties

 1.NSString *const nsfontattributename ( font):

The value corresponding to this property is a Uifont object. This property is used to change the font of a paragraph of text. If you do not specify this property, the default is 12-point Helvetica (Neue).

2.NSString *const Nsparagraphstyleattributename ( paragraph):

The value corresponding to this property is a Nsparagraphstyle object. This property applies multiple properties on a piece of text. If this property is not specified, the default paragraph property returned by the Defaultparagraphstyle method of Nsparagraphstyle is defaulted. Want to know nsparagraphstyle can self-study Baidu, here is not described in detail. Note: The NumberOfLines property of lable must be set to 0 for the paragraph style to take effect.

3.NSString *const nsforegroundcolorattributename ( font color):

The value corresponding to this property is a Uicolor object. This property is used to specify the font color for a paragraph of text. If you do not specify this property, the default is black.

4.NSString *const nsbackgroundcolorattributename ( font background color):

  The value corresponding to this property is a Uicolor object. This property is used to specify the background color for a paragraph of text. If you do not specify this property, the default is no background color.

5.NSString *const nsligatureattributename ( hyphen):

The value corresponding to this property is a NSNumber object (integer). Conjoined characters are some of the characters that are linked together, and they take a single element symbol. 0 means no conjoined characters. 1 indicates the use of the default conjoined character. 2 means that all conjoined symbols are used. The default value is 1 (note that IOS does not support a value of 2).

6.NSString *const nskernattributename ( word spacing):

The value corresponding to this property is a NSNumber object (integer). Conjoined characters are some of the characters that are linked together, and they take a single element symbol. 0 means no conjoined characters. 1 indicates the use of the default conjoined character. 2 means that all conjoined symbols are used. The default value is 1 (note that IOS does not support a value of 2).

7.NSString *const nsstrikethroughstyleattributename ( strikethrough):

The value corresponding to this property is a NSNumber object (integer). This value specifies whether to add strikethrough to the text, which is referred to as "underline Style Attributes". The default value is Nsunderlinestylenone.

8.NSString *const Nsunderlinestyleattributename ( underscore):

The value corresponding to this property is a NSNumber object (integer). This value specifies whether to underline the text, referring to "Underline Style Attributes". The default value is Nsunderlinestylenone.

9.NSString *const nsstrokecolorattributename ( edge color):

The value corresponding to this property is a Uicolor object. If this property is not specified (default), it is equivalent to Nsforegroundcolorattributename. Otherwise, the strikethrough or underline color is specified. See "Drawing attributedstrings that is both filled and stroked" for more details.

10.NSString *const nsstrokewidthattributename ( edge width):

The value corresponding to this property is a NSNumber object (decimal). The value changes the stroke width (relative to the percentage of the font size). The default is 0, which does not change. The positive number only changes the stroke width. Negative numbers also change the stroke and fill width of the text. For example, for common hollow words, this value is typically 3.0.

11.NSString *const Nsshadowattributename ( shadow):

The value corresponding to this property is a Nsshadow object. The default is nil.

12.NSString *const nsverticalglyphformattributename( layout-vertically):

The value corresponding to this property is a NSNumber object (integer). 0 indicates horizontal text. 1 indicates vertical text. In IOS, horizontal text is always used, and values other than 0 are undefined .

Threecode Example

Here are a few simple examples that are interesting to try the effects of the rest of the properties.

    • String not added property
    @" Hello world! ";         // Initialize the property string    nsmutableattributedstring *attrstr = [[Nsmutableattributedstring alloc] Initwithstring:[contentstr Stringbyappendingstring:@ "\ n"]];

    • Add a single attribute (for example, font color and font size)
    [Attrstr addattribute:nsforegroundcolorattributename Value:[uicolor Redcolor] Range:nsmakerange (66 )];    [Attrstr addattribute:nsfontattributename value:[uifont systemfontofsize:] Range:nsmakerange (0  )];

    • To add multiple properties using a property dictionary
[Attrstr addattributes:@{                             nsforegroundcolorattributename: [Uicolor Yellowcolor],                             Nsbackgroundcolorattributename: [Uicolor lightgraycolor]                             }                     range:nsmakerange (0,6) ];

    • Delete Property
    [Attrstr removeattribute:nsforegroundcolorattributename Range:nsmakerange (63)];    [Attrstr removeattribute:nsbackgroundcolorattributename Range:nsmakerange (51)];

Replace All strings

nsattributedstring *str = [[Nsattributedstring alloc] initwithstring:@ "123"]; [Attrstr Setattributedstring:str];

The above is part of the use of methods, everyone interested can try other methods of their own.

iOS learning Nsattributedstring (Rich Text)

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.