NSAttributedString (Rich Text) and nsattributedstring for iOS Learning
NSAttributedString is a string with attributes. It can easily show different fonts, font sizes, and font sizes in a string, you can also format paragraphs. Generally, you can perform some operations on the variable Rich Text (NSMutableAttributedString ).
I. Some common NSMutableAttributedString Methods
// Add a single text attribute to a certain range // parameter 1: character attribute name // parameter 2: attribute value // parameter 3: range-(void) addAttribute :( NSString *) name value :( id) value range :( nsange) range; // use the dictionary to add multiple text attributes in a certain range // parameter 1: attribute dictionary // parameter 2: range-(void) addAttributes :( NSDictionary <NSString *, id> *) attrs range :( nsange) range; // delete a certain text attribute in a certain range // parameter 1: character attribute name // parameter 2: range-(void) removeAttribute :( NSString *) name range: (nsange) range; // Replace the string in a certain range // parameter 1: range // parameter 2: string to be replaced-(void) replaceCharactersInRange :( nsange) range withAttributedString :( NSAttributedString *) attrString; // insert rich text at the corresponding badge // parameter 1: String to be inserted // parameter 2: location of the badge to be inserted-(void) insertAttributedString :( NSAttributedString *) attrString atIndex :( NSUInteger) loc; // concatenate a rich text to the end. // parameter: the string to be spliced-(void) appendAttributedString :( NSAttributedString *) attrString; // Delete the character in a certain range // parameter: range-(void) deleteCharactersInRange :( nsange) range; // replace all strings with another rich text string // parameter: The replaced Rich Text string-(void) setAttributedString :( NSAttributedString *) attrString;
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.
- No attribute is added to the string.
NSString * contentStr = @ "Hello World! "; // Initialization attribute string NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString: [contentStr stringByAppendingString: @" \ n "];
Add a single attribute (take the font color and font size as an example)
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6, 6)]; [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:25] range:NSMakeRange(0, 12)];
Add multiple attributes using the attribute dictionary
[attrStr addAttributes:@{ NSForegroundColorAttributeName : [UIColor yellowColor], NSBackgroundColorAttributeName : [UIColor lightGrayColor] } range:NSMakeRange(0,6)];
Delete attributes
[attrStr removeAttribute:NSForegroundColorAttributeName range:NSMakeRange(6, 3)]; [attrStr removeAttribute:NSBackgroundColorAttributeName range:NSMakeRange(5, 1)];
Replace all strings
NSAttributedString *str = [[NSAttributedString alloc] initWithString:@"123"]; [attrStr setAttributedString:str];
The above is the use of some methods. If you are interested, try other methods by yourself.