iOS使用NSMutableAttributedString實(shí)現(xiàn)富文本

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

1.實(shí)例化方法和使用方法

實(shí)例化方法:

使用字符串初始化

- (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類似

使用方法:

為某一范圍內(nèi)文字設(shè)置多個屬性

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

為某一范圍內(nèi)文字添加某個屬性

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

為某一范圍內(nèi)文字添加多個屬性

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

移除某范圍內(nèi)的某個屬性

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

2.常見的屬性及說明

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

3.使用實(shí)例

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

testLabel.backgroundColor=[UIColorlightGrayColor];

testLabel.textAlignment=NSTextAlignmentCenter;

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

[AttributedStraddAttribute:NSFontAttributeName

value:[UIFontsystemFontOfSize:16.0]

range:NSMakeRange(2,2)];

[AttributedStraddAttribute:NSForegroundColorAttributeName

value:[UIColorredColor]

range:NSMakeRange(2,2)];

testLabel.attributedText=AttributedStr;

[self.viewaddSubview:testLabel];

運(yùn)行效果:

另外,其他可以設(shè)置text的控件(如UIButton,UITextField)也都有該屬性,該文章不夠詳細(xì),只是簡單介紹,其他效果的實(shí)現(xiàn)參考API中更多的屬性及使用方法。
以下是簡單的一個封裝:

UILabel+ExtensionXY.h

/**

給label單獨(dú)設(shè)置changeText的文字顏色

*/

- (BOOL)setAttributedText:(NSString*)text FontF:(CGFloat)fontF Color:(UIColor*)color changeText:(NSString*)changeText;

/**

給label單獨(dú)設(shè)置range位子的文字顏色

*/

- (BOOL)setAttributedText:(NSString*)text FontF:(CGFloat)fontF Color:(UIColor*)color Range:(NSRange)range;


UILabel+ExtensionXY.m
/**

設(shè)置label中text中第一個changeText的字體大小顏色

@paramtextlabel的文本

@paramfontF字體大小

@paramcolor顏色

@paramchangeText需要改變的內(nèi)容

@return改變是否成功

*/

- (BOOL)setAttributedText:(NSString*)text FontF:(CGFloat)fontF Color:(UIColor*)color changeText:(NSString*)changeText

{

if(changeText.length) {

NSRangerange = [textrangeOfString:changeText];

if(range.location!=NSNotFound) {

return[selfsetAttributedText:textFontF:fontFColor:colorRange:range];

}

}

self.text= text;

returnNO;

}

/**

設(shè)置label中text中第一個changeText的字體大小顏色

@paramtextlabel的文本

@paramfontF字體大小

@paramcolor顏色

@paramrange要改變的地方

@return改變是否成功

*/

- (BOOL)setAttributedText:(NSString*)text FontF:(CGFloat)fontF Color:(UIColor*)color Range:(NSRange)range

{

if(!text) {

returnNO;

}

self.text= text;

NSMutableAttributedString*AttributedStr = [[NSMutableAttributedStringalloc]initWithAttributedString:self.attributedText];

if(range.location+ range.length> text.length) {

returnNO;

}else{

//NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:text];

if(!AttributedStr) {

AttributedStr = [[NSMutableAttributedStringalloc]initWithString:text];

}

if(fontF) {

[AttributedStraddAttribute:NSFontAttributeName

value:[UIFontsystemFontOfSize:fontF]

range:range];

}

if(color) {

[AttributedStraddAttribute:NSForegroundColorAttributeName

value:color

range:range];

}

self.attributedText= AttributedStr;

returnYES;

}

}

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

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

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