-(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;
運行效果:
另外,其他可以設置text 的控件(如UIButton,UITextField)也都有該屬性,該文章不夠詳細,只是簡單介紹,其他效果的實現參考API中更多的屬性及使用方法。