初探NSAttributedString和NSMutableAttributedString的使用


由於iOS7新出的NSTextStorge是NSMutableAttributedString的子類,所以要用好NSTextStorage,首先要學好NSMutableAttributedString和NSAttributedString。

按個人的理解,NSAttributedString是一個帶有屬性的字符串,通過該類可以靈活地操作和呈現多種樣式的文字數據。

因為是初步使用,所以基本上都是對照着文檔上的指導和介紹的方法來寫Demo。

首先是兩種類的初始化方法(基本上是相似的):

 

[cpp]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1.  1 // initWithString:  
     2 NSAttributedString *attributedString_str = [[NSAttributedString alloc] initWithString:@"attributedString"];  
     3 NSLog(@"%@", attributedString_str);  
     4 // textView.attributedText = attributedString_str;  
     5   
     6   
     7 // initWithAttributedString:  
     8 NSAttributedString *attributedString_atts = [[NSAttributedString alloc] initWithAttributedString:attributedString_str];  
     9 NSLog(@"%@", attributedString_atts);  
    10 // textView.attributedText = attributedString_atts;  
    11   
    12   
    13 // initWithString:attributes:  
    14 UIColor *backgroundColor = [UIColor blackColor];  
    15 NSNumber *baseLineOffset = [NSNumber numberWithFloat:20.0];  
    16 UIColor *foregroundColor = [UIColor whiteColor];  
    17 NSNumber *kern = [NSNumber numberWithFloat:5.0];  
    18 NSNumber *ligature = [NSNumber numberWithFloat:3.0];  
    19 NSURL *linkURL = [NSURL URLWithString:@"http://www.baidu.com"];  
    20 NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];  
    21 NSDictionary *attrsDic = @{NSForegroundColorAttributeName: foregroundColor,  
    22                            NSBackgroundColorAttributeName: backgroundColor,  
    23                            NSBaselineOffsetAttributeName: baseLineOffset,  
    24                            NSKernAttributeName: kern,  
    25                            NSLigatureAttributeName: ligature,  
    26                            NSLinkAttributeName: linkURL,  
    27                            NSUnderlineStyleAttributeName: underline  
    28                            };  
    29 NSAttributedString *attributedString_str_atts = [[NSAttributedString alloc] initWithString:@"http://www.baidu.com" attributes:attrsDic];  
    30 NSLog(@"%@", attributedString_str_atts);  
    31 // textView.attributedText = attributedString_str_atts;  
    32   
    33   
    34 // initWithFileURL:options:documentAttributes:error:  
    35 NSURL *fileURL = nil;  
    36 fileURL = [[NSBundle mainBundle] URLForResource:@"Dynamic Coloring" withExtension:@"rtf"];  
    37 NSAttributedString *attributedString_fileURL = [[NSAttributedString alloc] initWithFileURL:fileURL options:@{} documentAttributes:nil error:nil];  
    38 NSLog(@"%@", attributedString_fileURL);  
    39 // textView.attributedText = attributedString_fileURL;  
    40   
    41   
    42 // initWithData:options:documentAttributes:error:  
    43 fileURL = nil;  
    44 fileURL = [[NSBundle mainBundle] URLForResource:@"View Layout" withExtension:@"rtf"];  
    45 NSData *data = [[NSData alloc] initWithContentsOfURL:fileURL];  
    46 NSAttributedString *attributedString_data = [[NSAttributedString alloc] initWithData:data options:@{} documentAttributes:nil error:nil];  
    47 NSLog(@"%@", attributedString_data);  
    48 // textView.attributedText = attributedString_data;  
    49   
    50   
    51 // initWithAttributedString:  
    52 NSMutableAttributedString *mutableAttributedString_attrs = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString_fileURL];  

     

非常簡單。

 

 

由於NSAttributedString的屬性以字典的形式記錄,所以要弄清楚其中一些屬性對應的鍵值:


說說幾個自己使用上或者了解作用的。

NSBackgroundColorAttributeName:文字背景的顏色。

NSBaselineOffsetAttributeName:設置行距。

NSFontAttributeName:字體的樣式,必須設置NSFont作為Value,NSFont在iOS中不能使用,這個屬性的設置上我還不會。

NSForegroundColorAttributeName:文字顏色。

NSUnderlineStyleAttributeName:為文字添加下划線。必須設置NSNumber對象為Value。如:

 

[cpp]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];  

 

以上attribute在NSAttributedString和NSMutableAttributedString對象創建時可以設定,不同的是NSAttributedString對象在創建成功后其屬性便不可改變,而NSMutableAttributedString的屬性是可以改變的。如下所示:

 

[cpp]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1.  1 // Change NSMutableAttributedString  
     2 [mutableAttributedString_attrs beginEditing];  
     3 /* 
     4 // addAttributes:range: 
     5 [mutableAttributedString_attrs addAttributes:@{NSLinkAttributeName: @"www.baidu.com", 
     6                                                NSBackgroundColorAttributeName: [UIColor greenColor], 
     7                                                NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleDouble] 
     8                                                } 
     9                                        range:NSMakeRange(0, [attributedString_fileURL length])]; */  
    10 // addAttribute:value:range:  
    11 [mutableAttributedString_attrs addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleThick] range:NSMakeRange(0, 10)];  
    12 [mutableAttributedString_attrs addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithFloat:20.0] range:NSMakeRange(20, 100)];  
    13 [mutableAttributedString_attrs endEditing];  
    14 NSLog(@"%@", mutableAttributedString_attrs);  
    15  

     

在修改文字屬性的開頭和結尾要分別調用beginEditing和endEditing方法,這些方法可以在文字屬性發生變化時發送消息給事件監聽者。

可以為某個范圍內的文字單個地添加屬性key-value對,也可以添加一個屬性字典。
注意到在控制台輸出NSAttributedString或NSMutableAttributedString時,輸出的不僅是字符內容,還有對應的屬性值:

 

[cpp]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. 1 2013-08-11 16:40:44.737 AttributedString_i7_Demo[43468:a0b] http://www.baidu.com{  
    2     NSBackgroundColor = "UIDeviceWhiteColorSpace 0 1";  
    3     NSBaselineOffset = 20;  
    4     NSColor = "UIDeviceWhiteColorSpace 1 1";  
    5     NSKern = 5;  
    6     NSLigature = 3;  
    7     NSLink = "http://www.baidu.com";  
    8     NSUnderline = 1;  
    9 }  

     

 

 

也可以獲取某一段文字的屬性:

 

[cpp]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. 1 // get attribute  
    2 NSRange range = NSMakeRange(0, mutableAttributedString_attrs.length);  
    3 // attributesAtIndex:effectiveRange:  
    4 NSDictionary *dic = [mutableAttributedString_attrs attributesAtIndex:0 effectiveRange:&range];  
    5 NSLog(@"%@", [dic objectForKey:NSFontAttributeName]);  

     


如果要將NSMutableAttributedString對象賦值給NSAttributedString時,要使用copy方法:

[cpp]  view plain copy 在CODE上查看代碼片 派生到我的代碼片
 
  1. 1 textView.attributedText = [mutableAttributedString_attrs copy]; 

    以上是對NSAttributedString和NSMutableAttributedString最基本的使用(主要還是NSAttributedString),為了強化作為NSMutableAttributedString的子類NSTextStorage處理文本的能力,明顯在這兩個類中增加了許多新的成員,如NSTextAttachment,在這里沒有用上,必須繼續深入學習。

     

 


免責聲明!

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



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