iOS富文本字符串AttributedString具体用法

首先要创建一个带有属性的字符串NSMutableAttributedString

  1.  
    NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"这是一个富文本字符串"];
  2.  
    /* 其他几种创建方法
  3.  
    - (instancetype)initWithString:(NSString *)str;
  4.  
    - (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSString *, id> *)attrs;
  5.  
    - (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
  6.  
    */

如何设置这个字符串的属性

  1.  
    - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range; // 每次设置一个属性和它对应的值
  2.  
    - (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range; // 一次可以设置多个属性 属性和属性值以字典键值对形式进行设置
  3.  
    // range参数是设置要设置属性的字符串范围

常用的属性都有哪些

  • 字体 
    1.  
      [attrStr addAttribute:NSFontAttributeName
    2.  
      value:[UIFont systemFontOfSize:30.0f]
    3.  
      range:NSMakeRange(4, 3)];
     
    效果图
  • 颜色 
    1.  
      [attrStr addAttribute:NSForegroundColorAttributeName
    2.  
      value:[UIColor redColor]
    3.  
      range:NSMakeRange(4, 3)];
     
    效果图
    1.  
      [attrStr addAttribute:NSBackgroundColorAttributeName
    2.  
      value:[UIColor redColor]
    3.  
      range:NSMakeRange(4, 3)];
     
    效果图
  • 空心字 
    1.  
      [attrStr addAttribute:NSStrokeColorAttributeName
    2.  
      value:[UIColor redColor]
    3.  
      range:NSMakeRange(4, 3)]; // 设置描边颜色 要和NSStrokeWidthAttributeName设置描边宽度一起使用
    4.  
      [attrStr addAttribute:NSStrokeWidthAttributeName
    5.  
      value:@1.5
    6.  
      range:NSMakeRange(4, 3)];
     
    效果图
  • 间距 
    1.  
      [attrStr addAttribute:NSKernAttributeName
    2.  
      value:@10 // NSNumber
    3.  
      range:NSMakeRange(4, 3)];
     
    效果图
  • 倾斜 
    1.  
      [attrStr addAttribute:NSObliquenessAttributeName
    2.  
      value:@(0.5f) // 正值向右倾斜 负值向左倾斜
    3.  
      range:NSMakeRange(4, 3)];
     
    效果图
  • 拉伸、压缩 
    1.  
      [attrStr addAttribute:NSExpansionAttributeName
    2.  
      value:@(0.5f) // 正值横向拉伸 负值横向压缩
    3.  
      range:NSMakeRange(4, 3)];
     

     
    效果图
  • 连体 
    1.  
      NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"'flush' is a string!"];
    2.  
      [attrStr addAttribute:NSLigatureAttributeName
    3.  
      value:@1
    4.  
      range:NSMakeRange(1, 5)];
    5.  
      [attrStr addAttribute:NSFontAttributeName
    6.  
      value:[UIFont fontWithName:@"futura" size:30]
    7.  
      range:NSMakeRange(1, 5)];
 
效果图(注意fl两个字母的连接效果)
      • 基线偏移量

        1.  
          [attrStr addAttribute:NSBaselineOffsetAttributeName
        2.  
          value:@(10) // 正值上偏 负值下偏
        3.  
          range:NSMakeRange(4, 3)];
         
         
        效果图
      • 阴影

        1.  
          NSShadow *shadow = [[NSShadow alloc] init]; // NSShadow只有3个属性:阴影颜色,模糊半径和偏移
        2.  
          shadow.shadowOffset = CGSizeMake(3, 3); // 阴影偏移(X方向偏移和Y方向偏移)
        3.  
          shadow.shadowBlurRadius = 1.5; // 模糊半径
        4.  
          shadow.shadowColor = [UIColor redColor]; // 阴影颜色
        1.  
          [attrStr addAttribute:NSShadowAttributeName
        2.  
          value:shadow
        3.  
          range:NSMakeRange(4, 3)];
         
        效果图
      • 特殊效果 
        1.  
          [attrStr addAttribute:NSTextEffectAttributeName
        2.  
          value:NSTextEffectLetterpressStyle // NSString类型 目前只有NSTextEffectLetterpressStyle(凸版印刷效果)可用
        3.  
          range:NSMakeRange(4, 3)];
         
        效果图
      • 书写方向 
        1.  
          [attrStr addAttribute:NSWritingDirectionAttributeName
        2.  
          value:@[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)]
        3.  
          range:NSMakeRange(0, attrStr.length)];
         
        效果图
        1.  
          //NSWritingDirectionAttributeName 设置文字书写方向,取值为以下组合
        2.  
          // iOS9.0以前
        3.  
          //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)]
        4.  
          //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]
        5.  
          //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding)]
        6.  
          //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]
        7.  
          // iOS9.0以后
        8.  
          //@[@(NSWritingDirectionLeftToRight | NSWritingDirectionEmbedding)]
        9.  
          //@[@(NSWritingDirectionLeftToRight | NSWritingDirectionOverride)]
        10.  
          //@[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)]
        11.  
          //@[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)]
        12.  
          // NSWritingDirectionOverride 和 NSWritingDirectionEmbedding 是指定Unicode双向定义的格式控制算法(具体的没太搞清楚)
      • 横、竖排版 
        1.  
          [attrStr addAttribute:NSVerticalGlyphFormAttributeName
        2.  
          value:@0 // 0横向排版 1竖向排版 iOS中除了0以外都未定义,所以都为横向排版
        3.  
          range:NSMakeRange(0, attrStr.length)];
      • 下划线

        1.  
          [attrStr addAttribute:NSUnderlineStyleAttributeName
        2.  
          value:@(NSUnderlineStyleSingle)
        3.  
          range:NSMakeRange(4, 3)];
         
        效果图
        1.  
          [attrStr addAttribute:NSUnderlineColorAttributeName
        2.  
          value:[UIColor redColor]
        3.  
          range:NSMakeRange(4, 3)];
         
        效果图
        1.  
          typedef NS_ENUM(NSInteger, NSUnderlineStyle) {
        2.  
          NSUnderlineStyleNone = 0x00,
        3.  
          NSUnderlineStyleSingle = 0x01,
        4.  
          NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0) = 0x02,
        5.  
          NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0) = 0x09,
        6.  
          NSUnderlinePatternSolid NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0000,
        7.  
          NSUnderlinePatternDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0100,
        8.  
          NSUnderlinePatternDash NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0200,
        9.  
          NSUnderlinePatternDashDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0300,
        10.  
          NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0400,
        11.  
           
        12.  
          NSUnderlineByWord NS_ENUM_AVAILABLE(10_0, 7_0) = 0x8000
        13.  
          } NS_ENUM_AVAILABLE(10_0, 6_0);
      • 删除线

        1.  
          [attrStr addAttribute:NSStrikethroughStyleAttributeName
        2.  
          value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)
        3.  
          range:NSMakeRange(4, 3)];
         
        效果图
        1.  
          [attrStr addAttribute:NSStrikethroughColorAttributeName
        2.  
          value:[UIColor redColor]
        3.  
          range:NSMakeRange(4, 3)];
         
        效果图
      • 网址 
        1.  
          NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"这是一个网址http://www.jianshu.com/users/37f2920f6848字符串!"];
        2.  
          [attrStr addAttribute:NSLinkAttributeName
        3.  
          value:@"http://www.jianshu.com/users/37f2920f6848"
        4.  
          range:NSMakeRange(6, attrStr.length -6 -4)];
        5.  
          // label显示出来的连接是点击不了的 textView的是可以的 有响应的回调函数shouldInteractWithURL
         
        效果图
      • 图文混排 
        1.  
          NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"这是一个富文本字符串!"];
        2.  
          // 创建一个文字附件对象
        3.  
          NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
        4.  
          textAttachment.image = [UIImage imageNamed:@"11.png"]; //设置图片源
        5.  
          textAttachment.bounds = CGRectMake(0, -6, 30, 30); //设置图片位置和大小
        6.  
          // 将文字附件转换成属性字符串
        7.  
          NSAttributedString *attachmentAttrStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
        8.  
          // 将转换成属性字符串插入到目标字符串
        9.  
          [attrStr insertAttributedString:attachmentAttrStr atIndex:8];
         
        效果图
      • 段落 
        1.  
          NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        2.  
          // 行间距
        3.  
          paragraphStyle.lineSpacing = 15.f;
        4.  
          // 段落间距
        5.  
          paragraphStyle.paragraphSpacing = 30.f;
        6.  
          // 段落缩进像素
        7.  
          paragraphStyle.firstLineHeadIndent = 40.f;
        8.  
          // 整体缩进像素
        9.  
          paragraphStyle.headIndent = 15.f;
        10.  
          // 对齐方式
        11.  
          paragraphStyle.alignment = NSTextAlignmentLeft;
        12.  
          // 其他属性请自行查阅NSMutableParagraphStyle头文件
        1.  
          NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"这是一个富文本字符串。\n这是一个富文本字符串?\n这是一个富文本字符串!"];
        2.  
          [attrStr addAttribute:NSParagraphStyleAttributeName
        3.  
          value:paragraphStyle
        4.  
          range:NSMakeRange(0, attrStr.length)];
           
          效果图
posted @ 2021-08-15 13:17  孙富有(iOS工程师)  阅读(331)  评论(0编辑  收藏  举报