Initialize the

1. The commonly used:

NSMutableAttributedString *attrStr =[[NSMutableAttributedString alloc] initWithString:@"text"];

2. Convert the content to data, initialize it to the required type, and then convert it to rich text.

Such as: Load the HTML text, string will contain HTML tags, initialized to HTML type NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[@”text<\html>” dataUsingEncoding: NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

  • There are three key NSDocumentTypeDocumentAttribute NSPlainTextDocumentType — — — — — plain text NSRTFTextDocumentType – rich text NSRTFDTextDocumentType —– Rich text with attachments NSHTMLTextDocumentType —– This can load HTML text
  • NSDocumentTypeDocumentAttribute encoding conversion type NSPlainTextDocumentType NSRTFTextDocumentType NSRTFDTextDocumentType NSHTMLTextDocumentType

Rich text summary

Commonly used method

1. Gets the property information at the specified location and returns the same contiguous string range information as the property at the specified location.

  • -(NSDictionary*)attributesAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange

2. Comparison with another NSAttributedString returns a bool.

  • -(BOOL)isEqualToAttributedString:(NSAttributedString *)otherString

3. Obtain the object Substring

  • -(NSAttributedString *)attributedSubstringFromRange:(NSRange)aRange

4. Remove an attribute

  • -(void)removeAttribute:(NSString *)name range:(NSRange)range;

5. Specify a substring in the range to iterate over the attribute information overridden on the substring

  • -(void)enumerateAttribute:(NSString)attrName inRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (^)(id value,NSRange range,BOOL stop))block

6. Traverse the property information and range information in the specified range

  • -(void)enumerateAttributesInRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (^)(NSDictionaryattrs,NSRangerange,BOOL stop))block

7. Start editing

  • -(void)beginEditing;

8. Finish editing

  • -(void)endEditing;

9. Splicing attrString

  • -(void)appendAttributedString:(NSAttributedString *)attrString;

10. Insert attrString

  • -(void)insertAttributedString:(NSAttributedString *)attrString atIndex:(NSUInteger)loc;

11. Replace

  • -(void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString;

12. Delete characters

  • -(void)deleteCharactersInRange:(NSRange)range;

The Attribute properties

attribute role Value Object
NSFontAttributeName The font UIFont
NSParagraphStyleAttributeName Paragraph styles NSParagraphStyle
NSForegroundColorAttributeName The foreground UIColor
zebra NSBackgroundColorAttributeName The background color UIColor
NSObliquenessAttributeName tilt NSNumber
NSExpansionAttributeName flat NSNumber: flat ratio
NSStrokeWidthAttributeName Fill (Stroke, bold) NSNumber: hollow for positive numbers (stroke), fill for negative numbers (bold)
NSStrokeColorAttributeName Fill color UIColor
NSKernAttributeName spacing NSNumber
NSUnderlineStyleAttributeName The underline Enumeration: Default is NSUnderlineStyleNone
NSUnderlineColorAttributeName Underline color UIColor
NSStrikethroughStyleAttributeName Delete the line Enumeration: Default is NSUnderlineStyleNone
NSStrikethroughColorAttributeName Delete line color UIColor
NSLigatureAttributeName cursive NSNumber (0 or 1)
NSShadowAttributeName shadow NSShawdow
NSTextEffectAttributeName Set text special effects. Currently, only plate effects can be used NSString
NSAttachmentAttributeName Set text attachment, usually insert picture NSTextAttachment
NSLinkAttributeName link NSURL and nsstrings
NSBaselineOffsetAttributeName Base-line deviation NSNumber
NSWritingDirectionAttributeName The text direction NSArray<NSNumber *>*
NSVerticalGlyphFormAttributeName Horizontal or vertical text NSNumber at sign 1 or at sign (YES) vertical at sign 0 or at sign NO level

Paragraph styles (common)

NSMutableParagraphStyle *style = [[NSMutableParagraphStyle 
alloc]init];
style.lineSpacing = 10; / / space
style.paragraphSpacing = 20; / / distance
style.firstLineHeadIndent = 30; // Indent the first lineCopy the code

shadow

NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(2.2);
shadow.shadowColor = [UIColor orangeColor];
shadow.shadowBlurRadius = 1;Copy the code

usage

NSMutableAttributedString *attrStr = [[NSMutableAttributedString 
alloc] initWithString:label];
NSRange range = NSMakeRange(0, attrStr.length);
// Font slant
[attrStr addAttribute:NSObliquenessAttributeName value:@ (0.5) range:range];
// Make the font bold
[attrStr addAttribute:NSStrokeWidthAttributeName value:@ (- 3) range:range];
// Set the font
[attrStr addAttribute:NSFontAttributeName value:[UIFont 
fontWithName:@"JingLeiFontName" size:16] range:range];
label.attributedText = attrStr;Copy the code

Ps: USUALLY I don’t post many articles, this is my first work in Jane Book, but I still feel happy when I see my brother Mark. When I have time, I will upload more articles and study with my brothers

Refer to the link

Rich text NSAttributedString and NSMutableAttributedString NSAttributedString use AttributeString attribute