NSMutableAttributedString/NSAttributedString 富文本設(shè)置

//UILabel 設(shè)置NSAttributedString  
    NSMutableParagraphStyle *ps = [[NSMutableParagraphStyle alloc] init];  
    [ps setAlignment:NSTextAlignmentCenter];  
    NSDictionary *attribs = @{NSFontAttributeName: [UIFont fontWithName:@"Microsoft Yahei" size:45],NSParagraphStyleAttributeName:ps};  
    NSMutableAttributedString *attributedText =[[NSMutableAttributedString alloc] initWithString:super.text  
                                           attributes:attribs];  
    super.attributedText = attributedText;  




UITextView *lab = [LocalTexts objectAtIndex:j];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
paragraphStyle.lineHeightMultiple = 50.0f;
paragraphStyle.maximumLineHeight = 50.0f;
paragraphStyle.minimumLineHeight = 50.0f;
NSString *string = lab.text;
NSDictionary *ats = @{NSFontAttributeName : [UIFont fontWithName:@"DIN Medium" size:16.0f],NSParagraphStyleAttributeName : paragraphStyle, };
lab.attributedText = [[NSAttributedString alloc] initWithString:string attributes:ats];
//textview 設(shè)置行間距

NSString *const NSForegroundColorAttributeName;//值為UIColor,字體顏色,默認(rèn)為黑色。


NSString *const NSBackgroundColorAttributeName;//值為UIColor,字體背景色,默認(rèn)沒有。


NSString *const NSLigatureAttributeName;//值為整型NSNumber,連字屬性,一般中文用不到,在英文中可能出現(xiàn)相鄰字母連筆的情況。0為不連筆;1為默認(rèn)連筆,也是默認(rèn)值;2在ios 上不支持。


NSString *const NSKernAttributeName;//值為浮點(diǎn)數(shù)NSNumber,字距屬性,默認(rèn)值為0。


NSString *const NSStrikethroughStyleAttributeName;//值為整型NSNumber,可取值為


enum {


NSUnderlineStyleNone = 0×00,


NSUnderlineStyleSingle = 0×01,


};設(shè)置刪除線。
NSString *const NSUnderlineStyleAttributeName;//同上。設(shè)置下劃線。


NSString *const NSStrokeColorAttributeName;//值為UIColor,默認(rèn)值為nil,設(shè)置的屬性同F(xiàn)oregroundColor。


NSString *const NSStrokeWidthAttributeName;//值為浮點(diǎn)數(shù)NSNumber。設(shè)置比畫的粗細(xì)。


NSString *const NSShadowAttributeName;//值為NSShadow,設(shè)置比畫的陰影,默認(rèn)值為nil。


NSString *const NSVerticalGlyphFormAttributeName;//值為整型NSNumber,0為水平排版的字,1為垂直排版的字。 

// initWithString:  
NSAttributedString *attributedString_str = [[NSAttributedString alloc] initWithString:@"attributedString"];  
NSLog(@"%@", attributedString_str);  
// textView.attributedText = attributedString_str;  
  
  
// initWithAttributedString:  
NSAttributedString *attributedString_atts = [[NSAttributedString alloc] initWithAttributedString:attributedString_str];  
NSLog(@"%@", attributedString_atts);  
// textView.attributedText = attributedString_atts;  
  
  
// initWithString:attributes:  
UIColor *backgroundColor = [UIColor blackColor];  
NSNumber *baseLineOffset = [NSNumber numberWithFloat:20.0];  
UIColor *foregroundColor = [UIColor whiteColor];  
NSNumber *kern = [NSNumber numberWithFloat:5.0];  
NSNumber *ligature = [NSNumber numberWithFloat:3.0];  
NSURL *linkURL = [NSURL URLWithString:@"http://www.baidu.com"];  
NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];  
NSDictionary *attrsDic = @{NSForegroundColorAttributeName: foregroundColor,  
                           NSBackgroundColorAttributeName: backgroundColor,  
                           NSBaselineOffsetAttributeName: baseLineOffset,  
                           NSKernAttributeName: kern,  
                           NSLigatureAttributeName: ligature,  
                           NSLinkAttributeName: linkURL,  
                           NSUnderlineStyleAttributeName: underline  
                           };  
NSAttributedString *attributedString_str_atts = [[NSAttributedString alloc] initWithString:@"http://www.baidu.com" attributes:attrsDic];  
NSLog(@"%@", attributedString_str_atts);  
// textView.attributedText = attributedString_str_atts;  
  
  
// initWithFileURL:options:documentAttributes:error:  
NSURL *fileURL = nil;  
fileURL = [[NSBundle mainBundle] URLForResource:@"Dynamic Coloring" withExtension:@"rtf"];  
NSAttributedString *attributedString_fileURL = [[NSAttributedString alloc] initWithFileURL:fileURL options:@{} documentAttributes:nil error:nil];  
NSLog(@"%@", attributedString_fileURL);  
// textView.attributedText = attributedString_fileURL;  
  
  
// initWithData:options:documentAttributes:error:  
fileURL = nil;  
fileURL = [[NSBundle mainBundle] URLForResource:@"View Layout" withExtension:@"rtf"];  
NSData *data = [[NSData alloc] initWithContentsOfURL:fileURL];  
NSAttributedString *attributedString_data = [[NSAttributedString alloc] initWithData:data options:@{} documentAttributes:nil error:nil];  
NSLog(@"%@", attributedString_data);  
// textView.attributedText = attributedString_data;  
  
  
// initWithAttributedString:  
NSMutableAttributedString *mutableAttributedString_attrs = [[NSMutableAttributedString alloc] initWithAttributedString:attributedString_fileURL];  

由于NSAttributedString的屬性以字典的形式記錄,所以要弄清楚其中一些屬性對應(yīng)的鍵值:

屏幕快照 2016-06-01 下午7.50.50.png

NSBackgroundColorAttributeName:文字背景的顏色。

NSBaselineOffsetAttributeName:設(shè)置行距。

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

NSForegroundColorAttributeName:文字顏色。

NSUnderlineStyleAttributeName:為文字添加下劃線。必須設(shè)置NSNumber對象為Value。如

NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];

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

[![派生到我的代碼片](https://code.csdn.net/assets/ico_fork.svg)](https://code.csdn.net/snippets/84484/fork)

// Change NSMutableAttributedString  
[mutableAttributedString_attrs beginEditing];  
/* 
// addAttributes:range: 
[mutableAttributedString_attrs addAttributes:@{NSLinkAttributeName: @"www.baidu.com", 
                                               NSBackgroundColorAttributeName: [UIColor greenColor], 
                                               NSUnderlineStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleDouble] 
                                               } 
                                       range:NSMakeRange(0, [attributedString_fileURL length])]; */  
// addAttribute:value:range:  
[mutableAttributedString_attrs addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInt:NSUnderlineStyleThick] range:NSMakeRange(0, 10)];  
[mutableAttributedString_attrs addAttribute:NSBaselineOffsetAttributeName value:[NSNumber numberWithFloat:20.0] range:NSMakeRange(20, 100)];  
[mutableAttributedString_attrs endEditing];  
NSLog(@"%@", mutableAttributedString_attrs);  

在修改文字屬性的開頭和結(jié)尾要分別調(diào)用beginEditing和endEditing方法,這些方法可以在文字屬性發(fā)生變化時(shí)發(fā)送消息給事件監(jiān)聽者。
可以為某個(gè)范圍內(nèi)的文字單個(gè)地添加屬性key-value對,也可以添加一個(gè)屬性字典。
注意到在控制臺(tái)輸出NSAttributedString或NSMutableAttributedString時(shí),輸出的不僅是字符內(nèi)容,還有對應(yīng)的屬性值:

2016-06-1 16:40:44.737 AttributedString_i7_Demo[43468:a0b] http://www.baidu.com{  
    NSBackgroundColor = "UIDeviceWhiteColorSpace 0 1";  
    NSBaselineOffset = 20;  
    NSColor = "UIDeviceWhiteColorSpace 1 1";  
    NSKern = 5;  
    NSLigature = 3;  
    NSLink = "http://www.baidu.com";  
    NSUnderline = 1;  
}  

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

// get attribute  
NSRange range = NSMakeRange(0, mutableAttributedString_attrs.length);  
// attributesAtIndex:effectiveRange:  
NSDictionary *dic = [mutableAttributedString_attrs attributesAtIndex:0 effectiveRange:&range];  
NSLog(@"%@", [dic objectForKey:NSFontAttributeName]);  

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

textView.attributedText = [mutableAttributedString_attrs copy];  

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容