参考博客:http://blog.csdn.net/woaifen3344/article/details/38352099

       http://www.cnblogs.com/whyandinside/archive/2013/12/27/3493475.html

要使UILabel显示不同的字体,需要设置其 attributedText属性

该属性是NSMutableAttributedString/NSAttributedString类型;

NSAttributedString是一个带有属性的字符串,通过该类可以灵活地操作和呈现多种样式的文字数据。

NSAttributedString维护一个NSString,用来保存最原始的字符串,另有一个NSDictionary用来保存各个子串/字符的属性。

 1 NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"小路旁 堆积太多叶落 风吹动你和我 剩下沙丘荒漠"];  

2 [str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(0,5)]; //设置字体颜色

3 [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:30.0] range:NSMakeRange(0, 5)]; //设置字体字号和字体类别


4 UILabel *attrLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 320, 90)];
 5 attrLabel.attributedText = str;
6 attrLabel.numberOfLines = 0;


NSMakeRange(0, 5):第一个参数表示字符串中的第几个字符开始,第二个参数表示长度;

在iOS6.0以前版本实现这个效果,需要使用到一个第三方库TTTAttributedLabel,

CocoaPods安装和使用教程:

http://code4app.com/article/cocoapods-install-usage

podFile:pod 'TTTAttributedLabel'

  TTTAttributedLabel *lab;

 [lab setText:str];

 

 

 

posted on 2015-08-10 22:54  LDoctor  阅读(10526)  评论(0编辑  收藏  举报