IOS-NSAttributedString, nsattributedstring
1. Introduction to NSAttributeString
NSAttributedString is a string with attributes. It can easily show different fonts, font sizes, and font sizes in a string, you can also format a paragraph.
Ii. character attributes
1. NSString * const NSFontAttributeName (font ):
The value corresponding to this attribute is a UIFont object. This attribute is used to change the font of a text segment. If this attribute is not specified, the default value is 12-point Helvetica (Neue ).
2. NSString * const NSParagraphStyleAttributeName (paragraph ):
The value corresponding to this attribute is an NSParagraphStyle object. This attribute applies multiple attributes to a text segment. If this attribute is not specified, the default section attribute returned by the defaultParagraphStyle method of NSParagraphStyle is used by default. If you want to know about NSParagraphStyle, you can learn it from Baidu. I will not describe it in detail here. Note: The numberOfLines attribute 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 attribute is a UIColor object. This attribute is used to specify the font color of a text segment. If this attribute is not specified, the default value is black.
4. NSString * const NSBackgroundColorAttributeName (font background color ):
The value corresponding to this attribute is a UIColor object. This attribute is used to specify the background color of a piece of text. If this attribute is not specified, no background color is displayed by default.
5. NSString * const NSLigatureAttributeName (hyphen ):
The value corresponding to this attribute is an NSNumber object (integer ). Ins refer to some connected characters that use a single metacharacter. 0 indicates that no conjoined characters exist. 1 indicates that the default conjoin character is used. 2 indicates that all connected symbols are used. The default value is 1 (note that iOS does not support the value 2 ).
6. NSString * const NSKernAttributeName (font spacing ):
The value corresponding to this attribute is an NSNumber object (integer ). Ins refer to some connected characters that use a single metacharacter. 0 indicates that no conjoined characters exist. 1 indicates that the default conjoin character is used. 2 indicates that all connected symbols are used. The default value is 1 (note that iOS does not support the value 2 ).
7. NSString * const NSStrikethroughStyleAttributeName (strikethrough ):
The value corresponding to this attribute is an NSNumber object (integer ). This value specifies whether to add strikethrough to text. For this value, see "Underline Style Attributes ". The default value is NSUnderlineStyleNone.
8. NSString * const NSUnderlineStyleAttributeName (underline ):
The value corresponding to this attribute is an NSNumber object (integer ). This value specifies whether to Underline the text. For details about this value, see "Underline Style Attributes ". The default value is NSUnderlineStyleNone.
9. NSString * const NSStrokeColorAttributeName (edge color ):
The value corresponding to this attribute is a UIColor object. If this attribute is not specified (default), it is equivalent to NSForegroundColorAttributeName. Otherwise, it must be in the strikethrough or underline color. For more details, see "Drawing attributedstrings that are both filled and stroked ".
10. NSString * const NSStrokeWidthAttributeName (edge width ):
The value corresponding to this attribute is an NSNumber object (decimal number ). This value changes the stroke width (percentage relative to the font size ). The default value is 0. Only the width of the stroke is changed. The negative number simultaneously changes the stroke and fill width of the text. For example, for common hollow words, this value is usually 3.0.
11. NSString * const NSShadowAttributeName (Shadow ):
The value corresponding to this attribute is an NSShadow object. The default value is nil.
12. NSString * const NSVerticalGlyphFormAttributeName (horizontal and vertical layout ):
The value corresponding to this attribute is an NSNumber object (integer ). 0 indicates horizontal text. 1 indicates the vertical text. In iOS, horizontal text is always used. values other than 0 are not defined.
Iii. Sample Code
Here are a few simple examples. If you are interested, you can try the effects of other attributes.
// Example Lable UILabel * exLabel = [[UILabel alloc] initWithFrame: CGRectMake (100,100,200, 40)]; exLabel. textAlignment = NSTextAlignmentCenter; [self. view addSubview: exLabel]; NSString * exString = @ ": 150"; // Rich Text object NSMutableAttributedString * exAttributedString = [[NSMutableAttributedString alloc] initWithString: exString]; // Rich Text style // use the addAttribute method to set the style // The parameters are character attributes, values, and change the range // font color [exAttributedString addattriename: NSForegroundColorAttributeName value: [UIColor redColor] range: NSMakeRange (5, 4)]; // font size [exAttributedString addAttribute: NSFontAttributeName value: [UIFont systemFontOfSize: 25] range: NSMakeRange (5, 4)]; // background color [exAttributedString addattriename: background value: [UIColor grayColor] range: NSMakeRange (5, 4)]; // spacing [exAttributedString addattri: NSKernAttributeName value: [NSNumber numberWithInt: 5] range: NSMakeRange (5, 4)]; exLabel. attributedText = exAttributedString;
: