[轉] iOS使用NSMutableAttributedString 實現富文本(不同顏色字型、底線等)

來源:互聯網
上載者:User

標籤:

轉自:

在iOS開發中,常常會有一段文字顯示不同的顏色和字型,或者給某幾個文字加刪除線或底線的需求。之前在網上找了一些資料,有的是重繪UILabel的textLayer,有的是用html5實現的,都比較麻煩,而且很多UILabel的屬性也不起作用了,效果都不理想。後來瞭解到NSMuttableAttstring(帶屬性的字串),上面的一些需求都可以很簡便的實現。

  1. 執行個體化方法和使用方法

執行個體化方法:

使用字串初始化

- (id)initWithString:(NSString *)str;

例:

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天氣不錯呀"];

 

- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;

 

字典中存放一些屬性名稱和屬性值,如:

NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:

                                    [UIFontsystemFontOfSize:15.0],NSFontAttributeName,

                                    [UIColorredColor],NSForegroundColorAttributeName,

                                   NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];

 

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天氣不錯呀" attributes:attributeDict];

 

- (id)initWithAttributedString:(NSAttributedString *)attester;

使用NSAttributedString初始化,跟NSMutableString,NSString類似

 

使用方法:

為某一範圍內文字設定多個屬性

- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;

為某一範圍內文字添加某個屬性

- (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;

 

 

為某一範圍內文字添加多個屬性

- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;

移除某範圍內的某個屬性

- (void)removeAttribute:(NSString *)name range:(NSRange)range;

  1. 常見的屬性及說明

NSFontAttributeName  字型

NSParagraphStyleAttributeName  段落格式 

NSForegroundColorAttributeName  字型顏色

NSBackgroundColorAttributeName   背景顏色

NSStrikethroughStyleAttributeName 刪除線格式

NSUnderlineStyleAttributeName      底線格式

NSStrokeColorAttributeName        刪除線顏色

NSStrokeWidthAttributeName 刪除線寬度

NSShadowAttributeName  陰影

更多方法和屬性說明詳見蘋果官方說明文檔:

https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSMutableAttributedString_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003689

  1.  使用執行個體

   UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];

  testLabel.backgroundColor = [UIColor lightGrayColor];

  testLabel.textAlignment = NSTextAlignmentCenter;

  NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"今天天氣不錯呀"];

  [AttributedStr addAttribute:NSFontAttributeName

                        value:[UIFont systemFontOfSize:16.0]

                        range:NSMakeRange(2, 2)];

  [AttributedStr addAttribute:NSForegroundColorAttributeName

                        value:[UIColor redColor]

                        range:NSMakeRange(2, 2)];

  testLabel.attributedText = AttributedStr;

  [self.view addSubview:testLabel];

 

運行效果:

 

另外,其他可以設定text 的控制項(如UIButton,UITextField)也都有該屬性,該文章不夠詳細,只是簡單介紹,其他效果的實現參考API中更多的屬性及使用方法。

[轉] iOS使用NSMutableAttributedString 實現富文本(不同顏色字型、底線等)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.