iOS富文本字符串AttributedString詳解

http://www.jianshu.com/p/9ffcdc0003e0


首先要創建一個帶有屬性的字符串NSMutableAttributedString

NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:@"這是一個富文本字符串"]; 
/* 其他幾種創建方法
- (instancetype)initWithString:(NSString *)str;
- (instancetype)initWithString:(NSString *)str attributes:(nullable NSDictionary<NSString *, id> *)attrs;
- (instancetype)initWithAttributedString:(NSAttributedString *)attrStr;
*/

如何設置這個字符串的屬性

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range; // 每次設置一個屬性和它對應的值
- (void)addAttributes:(NSDictionary<NSString *, id> *)attrs range:(NSRange)range; // 一次可以設置多個屬性 屬性和屬性值以字典鍵值對形式進行設置
// range參數是設置要設置屬性的字符串範圍

常用的屬性都有哪些

  • 字體
    [attrStr addAttribute:NSFontAttributeName
                      value:[UIFont systemFontOfSize:30.0f]
                      range:NSMakeRange(4, 3)];

    效果圖
  • 顏色
    [attrStr addAttribute:NSForegroundColorAttributeName
                      value:[UIColor redColor]
                      range:NSMakeRange(4, 3)];

    效果圖
    [attrStr addAttribute:NSBackgroundColorAttributeName
                      value:[UIColor redColor]
                      range:NSMakeRange(4, 3)];

    效果圖
  • 空心字
    [attrStr addAttribute:NSStrokeColorAttributeName
                      value:[UIColor redColor]
                      range:NSMakeRange(4, 3)];  // 設置描邊顏色 要和NSStrokeWidthAttributeName設置描邊寬度一起使用
    [attrStr addAttribute:NSStrokeWidthAttributeName
                      value:@1.5
                      range:NSMakeRange(4, 3)];

    效果圖
  • 間距
    [attrStr addAttribute:NSKernAttributeName
                      value:@10                    // NSNumber
                      range:NSMakeRange(4, 3)];

    效果圖
  • 傾斜
    [attrStr addAttribute:NSObliquenessAttributeName
                      value:@(0.5f)          // 正值向右傾斜 負值向左傾斜
                      range:NSMakeRange(4, 3)];

    效果圖
  • 拉伸、壓縮
    [attrStr addAttribute:NSExpansionAttributeName
                      value:@(0.5f)      // 正值橫向拉伸 負值橫向壓縮
                      range:NSMakeRange(4, 3)];



    效果圖
  • 連體
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"'flush' is a string!"];
    [attrStr addAttribute:NSLigatureAttributeName
                      value:@1
                      range:NSMakeRange(1, 5)];
    [attrStr addAttribute:NSFontAttributeName
                      value:[UIFont fontWithName:@"futura" size:30]
                      range:NSMakeRange(1, 5)];

效果圖(注意fl兩個字母的連接效果)
  • 基線偏移量

    [attrStr addAttribute:NSBaselineOffsetAttributeName
                      value:@(10)   // 正值上偏 負值下偏
                      range:NSMakeRange(4, 3)];


    效果圖
  • 陰影

    NSShadow *shadow = [[NSShadow alloc] init];  // NSShadow只有3個屬性:陰影顏色,模糊半徑和偏移
    shadow.shadowOffset = CGSizeMake(3, 3);  // 陰影偏移(X方向偏移和Y方向偏移)
    shadow.shadowBlurRadius = 1.5;  // 模糊半徑
    shadow.shadowColor = [UIColor redColor];  // 陰影顏色
    [attrStr addAttribute:NSShadowAttributeName
                      value:shadow
                      range:NSMakeRange(4, 3)];

    效果圖
  • 特殊效果
    [attrStr addAttribute:NSTextEffectAttributeName
                      value:NSTextEffectLetterpressStyle  // NSString類型 目前只有NSTextEffectLetterpressStyle(凸版印刷效果)可用
                      range:NSMakeRange(4, 3)];

    效果圖
  • 書寫方向
    [attrStr addAttribute:NSWritingDirectionAttributeName
                      value:@[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)]
                      range:NSMakeRange(0, attrStr.length)];

    效果圖
    //NSWritingDirectionAttributeName 設置文字書寫方向,取值爲以下組合
    // iOS9.0以前
      //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionEmbedding)]
      //@[@(NSWritingDirectionLeftToRight | NSTextWritingDirectionOverride)]
      //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionEmbedding)]
      //@[@(NSWritingDirectionRightToLeft | NSTextWritingDirectionOverride)]
    // iOS9.0以後
      //@[@(NSWritingDirectionLeftToRight | NSWritingDirectionEmbedding)]
      //@[@(NSWritingDirectionLeftToRight | NSWritingDirectionOverride)]
      //@[@(NSWritingDirectionRightToLeft | NSWritingDirectionEmbedding)]
      //@[@(NSWritingDirectionRightToLeft | NSWritingDirectionOverride)]
    // NSWritingDirectionOverride 和 NSWritingDirectionEmbedding 是指定Unicode雙向定義的格式控制算法(具體的沒太搞清楚)
  • 橫、豎排版
    [attrStr addAttribute:NSVerticalGlyphFormAttributeName
                      value:@0   // 0橫向排版 1豎向排版 iOS中除了0以外都未定義,所以都爲橫向排版
                      range:NSMakeRange(0, attrStr.length)];
  • 下劃線

    [attrStr addAttribute:NSUnderlineStyleAttributeName
                      value:@(NSUnderlineStyleSingle)
                      range:NSMakeRange(4, 3)];

    效果圖
    [attrStr addAttribute:NSUnderlineColorAttributeName
                      value:[UIColor redColor]
                      range:NSMakeRange(4, 3)];

    效果圖
    typedef NS_ENUM(NSInteger, NSUnderlineStyle) {
      NSUnderlineStyleNone                                    = 0x00,
      NSUnderlineStyleSingle                                  = 0x01,
      NSUnderlineStyleThick NS_ENUM_AVAILABLE(10_0, 7_0)      = 0x02,
      NSUnderlineStyleDouble NS_ENUM_AVAILABLE(10_0, 7_0)     = 0x09,
      NSUnderlinePatternSolid NS_ENUM_AVAILABLE(10_0, 7_0)      = 0x0000,
      NSUnderlinePatternDot NS_ENUM_AVAILABLE(10_0, 7_0)        = 0x0100,
      NSUnderlinePatternDash NS_ENUM_AVAILABLE(10_0, 7_0)       = 0x0200,
      NSUnderlinePatternDashDot NS_ENUM_AVAILABLE(10_0, 7_0)    = 0x0300,
      NSUnderlinePatternDashDotDot NS_ENUM_AVAILABLE(10_0, 7_0) = 0x0400,
    
      NSUnderlineByWord NS_ENUM_AVAILABLE(10_0, 7_0)            = 0x8000
    } NS_ENUM_AVAILABLE(10_0, 6_0);
  • 刪除線

    [attrStr addAttribute:NSStrikethroughStyleAttributeName
                      value:@(NSUnderlinePatternSolid | NSUnderlineStyleSingle)
                      range:NSMakeRange(4, 3)];

    效果圖
    [attrStr addAttribute:NSStrikethroughColorAttributeName
                      value:[UIColor redColor]
                      range:NSMakeRange(4, 3)];

    效果圖
  • 網址
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"這是一個網址http://www.jianshu.com/users/37f2920f6848字符串!"];
    [attrStr addAttribute:NSLinkAttributeName
                      value:@"http://www.jianshu.com/users/37f2920f6848"
                      range:NSMakeRange(6, attrStr.length -6 -4)];
    // label顯示出來的連接是點擊不了的 textView的是可以的 有響應的回調函數shouldInteractWithURL

    效果圖
  • 圖文混排
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"這是一個富文本字符串!"];
    // 創建一個文字附件對象
    NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
    textAttachment.image = [UIImage imageNamed:@"11.png"];  //設置圖片源
    textAttachment.bounds = CGRectMake(0, -6, 30, 30);  //設置圖片位置和大小
    // 將文字附件轉換成屬性字符串
    NSAttributedString *attachmentAttrStr = [NSAttributedString attributedStringWithAttachment:textAttachment];
    // 將轉換成屬性字符串插入到目標字符串
    [attrStr insertAttributedString:attachmentAttrStr atIndex:8];

    效果圖
  • 段落
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
      // 行間距
      paragraphStyle.lineSpacing = 15.f;
      // 段落間距
      paragraphStyle.paragraphSpacing = 30.f;
      // 段落縮進像素
      paragraphStyle.firstLineHeadIndent = 40.f;
      // 整體縮進像素
      paragraphStyle.headIndent = 15.f;
      // 對齊方式
      paragraphStyle.alignment = NSTextAlignmentLeft;
    // 其他屬性請自行查閱NSMutableParagraphStyle頭文件
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:@"這是一個富文本字符串。\n這是一個富文本字符串?\n這是一個富文本字符串!"];
    [attrStr addAttribute:NSParagraphStyleAttributeName
                      value:paragraphStyle
                      range:NSMakeRange(0, attrStr.length)];

    效果圖

版權聲明:出自MajorLMJ技術博客的原創作品 ,轉載時必須註明出處及相應鏈接!


發佈了60 篇原創文章 · 獲贊 13 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章