NSMutableAttributedString/NSAttributedString

Equivalent to a string with attributes, text and text mix

Use steps:

Initialize a string. 2. Initialize attributes of a string. 3

1. Common methods for initializing strings:

- (instancetype)initWithString:(NSString *)str
- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary *)attrs;
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
+ (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment;
Copy the code

2. Common initialization properties:

NSString * const NSFontAttributeName NS_AVAILABLE(10_0, 6_0); / / UIFont, set the font nsstrings * const NSParagraphStyleAttributeName NS_AVAILABLE (10 _0, 6 _0); / / NSParagraphStyle, set paragraphs nsstrings * const NSForegroundColorAttributeName NS_AVAILABLE (10 _0, 6 _0); / / UIColor, set the font color nsstrings * const NSBackgroundColorAttributeName NS_AVAILABLE (10 _0, 6 _0); // UIColor, set the background color NSString * const NSLigatureAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber (integer); NSNumber (integer); 0 indicates no conjunctions; 1 is the default conjunctions, which is also the default value. 2 is not supported on ios. NSString * const NSKernAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber (floating point), set the word spacing, the letter compactness specifies the number of pixels used to adjust the word spacing. The effect of letter packing depends on the font. A value of 0 indicates that letter compacting is not used. The default value is 0 nsstrings * const NSStrikethroughStyleAttributeName NS_AVAILABLE (10 _0, 6 _0); / / NSNumber (integer), set the strikethrough nsstrings * const NSUnderlineStyleAttributeName NS_AVAILABLE (10 _0, 6 _0); / / NSNumber (integer), set the underline nsstrings * const NSStrokeColorAttributeName NS_AVAILABLE (10 _0, 6 _0); / / UIColor, set the border color, if this property is not specified (the default), is equal to NSForegroundColorAttributeName. Otherwise, specify a strikeout or underline color. NSString * const NSStrokeWidthAttributeName NS_AVAILABLE(10_0, 6_0); NSString * const NSShadowAttributeName NS_AVAILABLE(10_0, 6_0); / / NSShadow, shadows, set the default value is nil nsstrings * const NSAttachmentAttributeName NS_AVAILABLE (10 _0, 7 _0); // NSTextAttachment, default nil NSString * const NSLinkAttributeName NS_AVAILABLE(10_0, 7_0); // NSURL (preferred) or NSString NSString * const NSBaselineOffsetAttributeName NS_AVAILABLE(10_0, 7_0); / / NSNumber (float type), set the spacing nsstrings * const NSUnderlineColorAttributeName NS_AVAILABLE (10 _0, 7 _0); // UIColor NSString * const NSStrikethroughColorAttributeName NS_AVAILABLE(10_0, 7_0); // UIColor UIKIT_EXTERN NSString * const NSVerticalGlyphFormAttributeName NS_AVAILABLE(10_7, 6_0); //NSNumber (integer), 0 for horizontal type, 1 for vertical type.Copy the code

3. Assign attributes to strings

A simple example:
    NSMutableDictionary* arr = [NSMutableDictionary dictionary];
    arr[NSForegroundColorAttributeName] = [UIColor redColor];
    arr[NSBackgroundColorAttributeName] = [UIColor greenColor];
    arr[NSKernAttributeName] = @10;
    arr[NSUnderlineStyleAttributeName] = @1;
    NSMutableAttributedString* str = [[NSMutableAttributedString alloc]initWithString:@"My big knife is already hungry." attributes:arr];
    self.oneLabel.attributedText = str;
Copy the code
Effect:

To achieve simple text and text mix:
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]init];
    NSMutableAttributedString *childStr = [[NSMutableAttributedString alloc]initWithString:@"That's a lot of newbies,"];
    [str appendAttributedString:childStr];
    
    NSTextAttachment *attach = [[NSTextAttachment alloc]init];
    attach.image = [UIImage imageNamed:@"2"];
    attach.bounds = CGRectMake(0, -5, 20, 20);
    NSAttributedString *picStr = [NSAttributedString attributedStringWithAttachment:attach];
    [str appendAttributedString:picStr];
    
    NSAttributedString *childStr2 = [[NSAttributedString alloc]initWithString:@"Can the matching system find true equilibrium?"];
    [str appendAttributedString:childStr2];
    self.twoLabel.attributedText = str;

Copy the code
Effect:

Some that oneself sum up, have wrong place to criticize to correct please!

Github homepage