iOS-NSMutableAttributedString富文本的實現,ios富文本實現

來源:互聯網
上載者:User

iOS-NSMutableAttributedString富文本的實現,ios富文本實現

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:NSForegroundColorAttributeName14      15                           value:[UIColor redColor]16      17                           range:NSMakeRange(2, 2)];18     19     [AttributedStr addAttribute:NSBackgroundColorAttributeName20      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          //陰影

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

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

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.