標(biāo)簽內(nèi)顯示不同格式文本(不同字體, 不同顏色, 刪除線,下劃線)
NSAttributedString顯示字符串
實(shí)例化方法和使用方法
實(shí)例化方法:
使用字符串初始化
- (id)initWithString:(NSString *)str;
例:
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天氣不錯(cuò)呀"];
- (id)initWithString:(NSString *)str attributes:(NSDictionary *)attrs;
字典中存放一些屬性名和屬性值,如:
NSDictionary *attributeDict = [NSDictionarydictionaryWithObjectsAndKeys:
[UIFontsystemFontOfSize:15.0],NSFontAttributeName,
[UIColorredColor],NSForegroundColorAttributeName,
NSUnderlineStyleAttributeName,NSUnderlineStyleSingle,nil];
NSMutableAttributedString *AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"今天天氣不錯(cuò)呀" attributes:attributeDict];
- (id)initWithAttributedString:(NSAttributedString *)attester;
使用NSAttributedString初始化,跟NSMutableString,NSString類似
使用方法:
為某一范圍內(nèi)文字設(shè)置多個(gè)屬性
- (void)setAttributes:(NSDictionary *)attrs range:(NSRange)range;
為某一范圍內(nèi)文字添加某個(gè)屬性 - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)range;
為某一范圍內(nèi)文字添加多個(gè)屬性
- (void)addAttributes:(NSDictionary *)attrs range:(NSRange)range;
移除某范圍內(nèi)的某個(gè)屬性 - (void)removeAttribute:(NSString *)name range:(NSRange)range;
常見的屬性及說明
NSFontAttributeName 字體
NSParagraphStyleAttributeName 段落格式
NSForegroundColorAttributeName 字體顏色
NSBackgroundColorAttributeName 背景顏色
NSStrikethroughStyleAttributeName 刪除線格式
NSUnderlineStyleAttributeName 下劃線格式
NSStrokeColorAttributeName 刪除線顏色
NSStrokeWidthAttributeName 刪除線寬度
NSShadowAttributeName 陰影