iOS - NSMutableAttributedString富文本的實現


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

(一)效果圖:

  

(二)代碼:

 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          // 字體

  NSParagraphStyleAttributeName    // 段落格式 

  NSForegroundColorAttributeName    // 字體顏色

  NSBackgroundColorAttributeName   // 背景顏色

  NSStrikethroughStyleAttributeName  // 刪除線格式

  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

  (以上便是對相關知識的相關介紹和理解,還希望大家相互補充共同進步)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM