Initialization
1. Commonly used:
NSMutableAttributedString *attrStr =[[NSMutableAttributedString alloc] initWithString:@"text"];
2. The edited content will be converted to data, initialized to the required type, and then converted to rich text.
For example, load HTML text, initialize a string containing HTML tags to an HTML type
NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[@"<html>text<\html>" dataUsingEncoding: NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
- NSDocument Type Document Attribute has three key s
NSPlain Text Document Type - --- Ordinary Text
NSRTFTextDocument Type - - - Rich Text
NSRTFDTextDocument Type - Rich Text with Attachments
NSHTML Text Document Type - This text can be loaded in HTML format - NSDocumentType DocumentAttribute Coding Conversion Type
NSPlainTextDocumentType
NSRTFTextDocumentType
NSRTFDTextDocumentType
NSHTMLTextDocumentType
Rich text summary
common method
1.Gets attribute information at a specified location,It also returns the range information of the same and continuous string as the specified location attribute.
- -(NSDictionary*)attributesAtIndex:(NSUInteger)index effectiveRange:(NSRangePointer)aRange
2.With another NSAttributedString Object Contrast Returns bool Value.
- -(BOOL)isEqualToAttributedString:(NSAttributedString *)otherString
3.Getting objects Substring
- -(NSAttributedString *)attributedSubstringFromRange:(NSRange)aRange
4.Remove an attribute
- -(void)removeAttribute:(NSString *)name range:(NSRange)range;
5.Substring in the specified range,Attribute information overlaid on traversal substrings
- -(void)enumerateAttribute:(NSString)attrName inRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (^)(id value,NSRange range,BOOL stop))block
6.Traversing the attribute information and within the specified range range information
- -(void)enumerateAttributesInRange:(NSRange)enumerationRange options:(NSAttributedStringEnumerationOptions)opts usingBlock:(void (^)(NSDictionaryattrs,NSRangerange,BOOL stop))block
7.Start editing
- -(void)beginEditing;
8.End 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;
Attribute attribute
attribute | Effect | value corresponding object |
---|---|---|
NSFontAttributeName | Typeface | UIFont |
NSParagraphStyleAttributeName | paragraph style | NSParagraphStyle |
NSForegroundColorAttributeName | Foreground color | UIColor |
zebra NSBackgroundColorAttributeName | background color | UIColor |
NSObliquenessAttributeName | tilt | NSNumber |
NSExpansionAttributeName | Delayering | NSNumber: Flat Ratio |
NSStrokeWidthAttributeName | Filling (edge drawing, thickening) | NSNumber: Positive number is hollow (stroke edge), negative number is filled (bold) |
NSStrokeColorAttributeName | fill color | UIColor |
NSKernAttributeName | spacing | NSNumber |
NSUnderlineStyleAttributeName | Underline | Enumeration: NSUnderline StyleNone by default |
NSUnderlineColorAttributeName | Underline colour | UIColor |
NSStrikethroughStyleAttributeName | Delete line | Enumeration: NSUnderline StyleNone by default |
NSStrikethroughColorAttributeName | Delete line color | UIColor |
NSLigatureAttributeName | joined-up | NSNumber(0 or 1) |
NSShadowAttributeName | shadow | NSShawdow |
NSTextEffectAttributeName | Setting text special effects, currently only plate printing effect can be used | NSString |
NSAttachmentAttributeName | Set up text attachments, commonly insert pictures | NSTextAttachment |
NSLinkAttributeName | link | NSURL and NSString |
NSBaselineOffsetAttributeName | Baseline migration | NSNumber |
NSWritingDirectionAttributeName | Text orientation | NSArray<NSNumber *>* |
NSVerticalGlyphFormAttributeName | Horizontal or vertical text | NSNumber @1 or @ (YES) Vertical @0 or @ (NO) Level |
Paragraph Style (Commonly Used)
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle
alloc]init];
style.lineSpacing = 10; // Row spacing
style.paragraphSpacing = 20; // Segment distance
style.firstLineHeadIndent = 30; // text-indent
shadow
NSShadow *shadow = [[NSShadow alloc]init];
shadow.shadowOffset = CGSizeMake(2, 2);
shadow.shadowColor = [UIColor orangeColor];
shadow.shadowBlurRadius = 1;
usage
NSMutableAttributedString *attrStr = [[NSMutableAttributedString
alloc] initWithString:label];
NSRange range = NSMakeRange(0, attrStr.length);
// Font tilt
[attrStr addAttribute:NSObliquenessAttributeName value:@(0.5) range:range];
// font-weight
[attrStr addAttribute:NSStrokeWidthAttributeName value:@(-3) range:range];
// Setting fonts
[attrStr addAttribute:NSFontAttributeName value:[UIFont
fontWithName:@"JingLeiFontName" size:16] range:range];
label.attributedText = attrStr;
ps: Normally, I seldom publish articles. This is my maiden work in a brief book. But I am happy to see my brothers mark. When I have time, I will upload more articles and study with my brothers.
Author: wxzhi
Link: http://www.jianshu.com/p/2763a57c93c1
Source: Brief Book
Copyright belongs to the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.