IOS之NSAttributedString和NSMutableAttributedString

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;

  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值