ios6以前在一個字符串中若是也讓某個字體高亮或者特殊顯示(如: 關注[ 101]),須要用單獨一個的標籤進行顯示,或者利用CoreText進行字體繪繪製,很是麻煩;java
如今IOS6 中TextView,label,textField中新增了這樣的一個屬性NSAttributedString 只能應用IOS6ios
@property(nonatomic,copy) NSAttributedString *attributedText NS_AVAILABLE_IOS(6_0); // default is nil
利用NSAttributedString構建一個新的屬性字符串,你就能夠對他進行操做,刪除,增長,字體格式化,設置某個字符前景色,字體顏色等...字體
如上圖所示,直接上代碼atom
- (void)setupTextView { _textView = [[UITextView alloc] initWithFrame:self.view.frame]; self.textView.textColor = [UIColor blackColor]; self.textView.font = [UIFont fontWithName:@"Arial" size:18.0]; self.textView.delegate = self; self.textView.backgroundColor = [UIColor whiteColor]; self.textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; NSString *textToAdd = @"Now is the time for all good developers to come to serve their country.\n\nNow is the time for all good developers to come to serve their country.\r\rThis text view can also use attributed strings."; NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:textToAdd]; // make red text [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange([attrString length] - 19, 19)]; // make blue text [attrString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange([attrString length] - 23, 3)]; [attrString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:1] range:NSMakeRange([attrString length] - 23, 3)]; [self.textView setAttributedText:attrString]; self.textView.returnKeyType = UIReturnKeyDefault; self.textView.scrollEnabled = YES; [self.view addSubview:self.textView]; }
NSForegroundColorAttributeName // 表明字體的前景色 下面全部進行字體格式化的標籤spa
/* Predefined character attributes for text. If the key is not in the dictionary, then use the default values as described below.code
*/orm
UIKIT_EXTERN NSString *const NSFontAttributeName NS_AVAILABLE_IOS(6_0); // UIFont, default Helvetica(Neue) 12ci
UIKIT_EXTERN NSString *const NSParagraphStyleAttributeName NS_AVAILABLE_IOS(6_0); // NSParagraphStyle, default defaultParagraphStyle字符串
UIKIT_EXTERN NSString *const NSForegroundColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default blackColorstring
UIKIT_EXTERN NSString *const NSBackgroundColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default nil: no background
UIKIT_EXTERN NSString *const NSLigatureAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 1: default ligatures, 0: no ligatures, 2: all ligatures (Note: 2 is unsupported on iOS)
UIKIT_EXTERN NSString *const NSKernAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing floating point value, in points; amount to modify default kerning. 0 means kerning is disabled. (note: values other than nil and 0 are unsupported on iOS)
UIKIT_EXTERN NSString *const NSStrikethroughStyleAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 0: no strikethrough
UIKIT_EXTERN NSString *const NSUnderlineStyleAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing integer, default 0: no underline
UIKIT_EXTERN NSString *const NSStrokeColorAttributeName NS_AVAILABLE_IOS(6_0); // UIColor, default nil: same as foreground color
UIKIT_EXTERN NSString *const NSStrokeWidthAttributeName NS_AVAILABLE_IOS(6_0); // NSNumber containing floating point value, in percent of font point size, default 0: no stroke; positive for stroke alone, negative for stroke and fill (a typical value for outlined text would be 3.0)
UIKIT_EXTERN NSString *const NSShadowAttributeName NS_AVAILABLE_IOS(6_0); // NSShadow, default nil: no shadow
/* An NSNumber containing an integer value. 0 means horizontal text. 1 indicates vertical text. If not specified, it could follow higher-level vertical orientation settings. Currently on iOS, it's always horizontal. The behavior for any other value is undefined.
*/
UIKIT_EXTERN NSString *const NSVerticalGlyphFormAttributeName NS_AVAILABLE_IOS(6_0);