iOS - NSMutableAttributedString富文本的實現

NSMutableAttributedString繼承於NSAttributedString(帶屬性的字符串)可以簡單快速實現富文本的效果;很少說直接上效果圖和代碼,通俗易懂:html

(一)效果圖:ios

  

(二)代碼:app

 1     UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, [[UIScreen mainScreen] bounds].size.width, 30)];
 2     
 3     testLabel.textAlignment = NSTextAlignmentCenter;
 4     
 5     NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"猴年大吉,新春快樂!"];
 6     
 7     [AttributedStr addAttribute:NSFontAttributeName
 8      
 9                           value:[UIFont systemFontOfSize:26.0]
10      
11                           range:NSMakeRange(2, 2)];
12     
13     [AttributedStr addAttribute:NSForegroundColorAttributeName
14      
15                           value:[UIColor redColor]
16      
17                           range:NSMakeRange(2, 2)];
18     
19     [AttributedStr addAttribute:NSBackgroundColorAttributeName
20      
21                           value:[UIColor redColor]
22      
23                           range:NSMakeRange(7, 2)];
24     
25     testLabel.attributedText = AttributedStr;
26     
27     [self.view addSubview:testLabel];

(三)常見的屬性及說明:字體

  NSFontAttributeName          // 字體spa

  NSParagraphStyleAttributeName    // 段落格式 3d

  NSForegroundColorAttributeName    // 字體顏色code

  NSBackgroundColorAttributeName   // 背景顏色htm

  NSStrikethroughStyleAttributeName  // 刪除線格式blog

  NSUnderlineStyleAttributeName        //下劃線格式繼承

  NSStrokeColorAttributeName            //刪除線顏色

  NSStrokeWidthAttributeName       //刪除線寬度

  NSShadowAttributeName          //陰影

(四)推薦好友博文地址,介紹的很詳細:

  http://www.cnblogs.com/goodboy-heyang/p/5143135.html#3361725

(五)蘋果官方說明文檔:

  https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/index.html

  (以上即是對相關知識的相關介紹和理解,還但願你們相互補充共同進步)

相關文章
相關標籤/搜索