NSAttributedString是一个带有属性的字符串,通过该类可以灵活地操作和呈现多种样式的文字数据。官方的API 为我们提供了UILabel类的attributedText, 使用不同颜色和不同字体的字符串。
如果想在iOS6.0以前版本实现这个效果,需要使用到一个第三方库TTTAttributedLabel,同时还有导入CoreText.frame框架.
// initWithString:
NSAttributedString *attributedString_str = [[NSAttributedString alloc] initWithString:@"attributedString"];
NSLog(@"%@", attributedString_str);
textView.attributedText = attributedString_str;
// initWithAttributedString:
NSAttributedString *attributedString_atts = [[NSAttributedString alloc] initWithAttributedString:attributedString_str];
NSLog(@"%@", attributedString_atts);
textView.attributedText = attributedString_atts;
// initWithString:attributes:
UIColor *backgroundColor = [UIColor blackColor];
NSNumber *baseLineOffset = [NSNumber numberWithFloat:20.0];
UIColor *foregroundColor = [UIColor whiteColor];
NSNumber *kern = [NSNumber numberWithFloat:5.0];
NSNumber *ligature = [NSNumber numberWithFloat:3.0];
NSURL *linkURL = [NSURL URLWithString:@"http://www.baidu.com"];
NSNumber *underline = [NSNumber numberWithInt:NSUnderlineStyleSingle];
NSDictionary *attrsDic = @{NSForegroundColorAttributeName: foregroundColor,
NSBackgroundColorAttributeName: backgroundColor,
NSBaselineOffsetAttributeName: baseLineOffset,
NSKernAttributeName: kern,
NSLigatureAttributeName: ligature,
NSLinkAttributeName: linkURL,
NSUnderlineStyleAttributeName: underline
};
NSAttributedString *attributedString_str_atts = [[NSAttributedString alloc] initWithString:@"http://www.baidu.com" attributes:attrsDic];
NSLog(@"%@", attributedString_str_atts);
textView.attributedText = attributedString_str_atts;
// initWithFileURL:options:documentAttributes:error:
NSURL *fileURL = nil;
fileURL = [[NSBundle mainBundle] URLForResource:@"Dynamic Coloring" withExtension:@"rtf"];
NSAttributedString *attributedString_fileURL = [[NSAttributedString alloc] initWithFileURL:fileURL options:@{} documentAttributes:nil error:nil];
NSLog(@"%@", attributedString_fileURL);
textView.attributedText = attributedString_fileURL;
// initWithData:options:documentAttributes:error:
fileURL = nil;
fileURL = [[NSBundle mainBundle] URLForResource:@"View Layout" withExtension:@"rtf"];
NSData *data = [[NSData alloc] initWithContentsOfURL:fileURL];
NSAttributedString *attributedString_data = [[NSAttributedString alloc] initWithData:data options:@{} documentAttributes:nil error:nil];
NSLog(@"%@", attributedString_data);
textView.attributedText = attributedString_data;
self.title = @"For iOS 6 & later";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(6,12)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(19,6)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(0, 5)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(6, 12)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(19, 6)];
attrLabel.attributedText = str;