屬性化字符串,用到的時候問題一大堆,一一記下。函數
UITextView , UILabel , 均可以使用屬性化字符串.字體
以UILabel爲例,以下代碼.spa
//函數做用:添加統計數據label; 參數text是數據(包括單位),如:3600分; 參數unit是單位,如: 分 - (void)addLabelBy:(CGRect)frame withText:(NSString *)text inView:(UIView *)view unit:(NSString *)unit size:(CGFloat)high{ //屬性化字符串 NSMutableAttributedString *atrString=[[NSMutableAttributedString alloc]initWithString:text]; NSUInteger length=[atrString length]; //不一樣字體 UIFont *dataFont=[UIFont fontWithName:@"Georgia-Bold" size:high]; UIFont *unitFont=[UIFont systemFontOfSize:high/2]; //把數據和單位設置成 不一樣字體和顏色 [atrString addAttribute:NSFontAttributeName value:dataFont range:NSMakeRange(0, length)]; [atrString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, length)]; [atrString addAttribute:NSFontAttributeName value:unitFont range:[text rangeOfString:unit]]; [atrString addAttribute:NSForegroundColorAttributeName value:[UIColor brownColor] range:[text rangeOfString:unit]]; //添加label UILabel *label=[[UILabel alloc]initWithFrame:frame]; label.attributedText=atrString; label.textAlignment=NSTextAlignmentCenter; [view addSubview:label]; }
3.在UITextView 中涉及到換行,要加\n,對屬性化字符串同樣有效code
4.順便說下,UITextView頂部空白,須要設置self.automaticallyAdjustsScrollViewInsets = NO;orm
5.屬性化字符串 屬性列舉ci
NSString *const NSFontAttributeName;(字體)
字符串
NSString *const NSParagraphStyleAttributeName;(段落)
it
NSString *const NSForegroundColorAttributeName;(字體顏色)
table
NSString *const NSBackgroundColorAttributeName;(字體背景色)
class
NSString *const NSLigatureAttributeName;(連字符)
NSString *const NSKernAttributeName;(字間距)
NSString *const NSStrikethroughStyleAttributeName;(刪除線)
NSString *const NSUnderlineStyleAttributeName;(下劃線)
NSString *const NSStrokeColorAttributeName;(邊線顏色)
NSString *const NSStrokeWidthAttributeName;(邊線寬度)
NSString *const NSShadowAttributeName;(陰影)(橫豎排版)
NSString *const NSVerticalGlyphFormAttributeName;