NSMutableAttributedString/NSAttributedString

NSMutableAttributedString/NSAttributedString

相当于带属性的字符串,实现图文混排

使用步骤:

1.初始化字符串
2.初始化字符串的属性
3.将属性赋值给字符串

1.初始化字符串的常用方法:

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

2.常见的初始化属性:

NSString * const NSFontAttributeName NS_AVAILABLE(10_0, 6_0);  // UIFont,设置字体

NSString * const NSParagraphStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSParagraphStyle, 设置段落

NSString * const NSForegroundColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, 设置字体颜色

NSString * const NSBackgroundColorAttributeName NS_AVAILABLE(10_0, 6_0); // UIColor, 设置背景颜色

NSString * const NSLigatureAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber(整型),连字属性,一般中文用不到,在英文中可能出现相邻字母连笔的情况。0为不连笔;1为默认连笔,也是默认值;2在ios 上不支持。

NSString * const NSKernAttributeName NS_AVAILABLE(10_0, 6_0);  // NSNumber (浮点型),设置字间距,字母紧排指定了用于调整字距的像素点数。字母紧排的效果依赖于字体。值为 0 表示不使用字母紧排。默认值为0

NSString * const NSStrikethroughStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber(整型),设置删除线

NSString * const NSUnderlineStyleAttributeName NS_AVAILABLE(10_0, 6_0); // NSNumber (整型),设置下划线

NSString * const NSStrokeColorAttributeName NS_AVAILABLE(10_0, 6_0);  // UIColor,设置边线颜色,如果该属性不指定(默认),则等同于NSForegroundColorAttributeName。否则,指定为删除线或下划线颜色。

NSString * const NSStrokeWidthAttributeName NS_AVAILABLE(10_0, 6_0);  // NSNumber(浮点型),设置边线宽度

NSString * const NSShadowAttributeName NS_AVAILABLE(10_0, 6_0);  // NSShadow, 设置阴影,默认为nil

NSString * const NSAttachmentAttributeName NS_AVAILABLE(10_0, 7_0);  // NSTextAttachment, 默认为nil

NSString * const NSLinkAttributeName NS_AVAILABLE(10_0, 7_0);   // NSURL (preferred) or NSString

NSString * const NSBaselineOffsetAttributeName NS_AVAILABLE(10_0, 7_0);   // NSNumber (浮点型),设置行距

NSString * 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(整型),0为水平排版的字,1为垂直排版的字。

3.将属性赋值给字符串

简单实例:
    NSMutableDictionary* arr = [NSMutableDictionary dictionary];
    arr[NSForegroundColorAttributeName] = [UIColor redColor];
    arr[NSBackgroundColorAttributeName] = [UIColor greenColor];
    arr[NSKernAttributeName] = @10;
    arr[NSUnderlineStyleAttributeName] = @1;
    NSMutableAttributedString* str = [[NSMutableAttributedString alloc]initWithString:@"我的大刀早已饥渴难耐了" attributes:arr];
    self.oneLabel.attributedText = str;
效果:
效果.png
实现简单的图文混排:
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc]init];
    NSMutableAttributedString *childStr = [[NSMutableAttributedString alloc]initWithString:@"菜鸟真多,"];
    [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:@"匹配系统能找到真正的平衡么"];
    [str appendAttributedString:childStr2];
    self.twoLabel.attributedText = str;

效果:
图文混排.png

自己总结的一些,有不对的地方请批评指正!

你可能感兴趣的:(NSMutableAttributedString/NSAttributedString)