在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串,在iOS  以后我们可以很轻松的实现这一点,官方的API 为我们提供了UILabel类的attributedText, 使用不同颜色和不同字体的字符串,我们可以使用NSAttributedText 和 NSMutableAttributedText 类来实现。

现实代码:

@interface ViewController : UIViewController
@property (nonatomic, strong) IBOutlet UILabel *attrLabel;
- (IBAction)next:(id)sender;
@end
.m文件 在viewDidLoad方法中添加以下代码: self.title = @"For iOS 6 & later";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String"];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSMakeRange(,)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(,)];
[str addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(,)];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:30.0] range:NSMakeRange(, )];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"HelveticaNeue-Bold" size:30.0] range:NSMakeRange(, )];
[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Courier-BoldOblique" size:30.0] range:NSMakeRange(, )];
attrLabel.attributedText = str;
NSString *const NSForegroundColorAttributeName;//值为UIColor,字体颜色,默认为黑色。

NSString *const NSBackgroundColorAttributeName;//值为UIColor,字体背景色,默认没有。

NSString *const NSLigatureAttributeName;//值为整型NSNumber,连字属性,一般中文用不到,在英文中可能出现相邻字母连笔的情况。0为不连笔;1为默认连笔,也是默认值;2在ios 上不支持。

NSString *const NSKernAttributeName;//值为浮点数NSNumber,字距属性,默认值为0。

NSString *const NSStrikethroughStyleAttributeName;//值为整型NSNumber,可取值为

enum {

NSUnderlineStyleNone = ×,

NSUnderlineStyleSingle = ×,

};设置删除线。

NSString *const NSUnderlineStyleAttributeName;//同上。设置下划线。

NSString *const NSStrokeColorAttributeName;//值为UIColor,默认值为nil,设置的属性同ForegroundColor。

NSString *const NSStrokeWidthAttributeName;//值为浮点数NSNumber。设置比画的粗细。

NSString *const NSShadowAttributeName;//值为NSShadow,设置比画的阴影,默认值为nil。

NSString *const NSVerticalGlyphFormAttributeName;//值为整型NSNumber,0为水平排版的字,1为垂直排版的字。

NSMutableAttributedString iOS 在UILabel显示不同的字体和颜色(转)的更多相关文章

  1. iOS 在UILabel显示不同的字体和颜色(转)

    转自:http://my.oschina.net/CarlHuang/blog/138363 在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串, ...

  2. iOS 在UILabel显示不同的字体和颜色

    转自:http://my.oschina.net/CarlHuang/blog/138363 在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串, ...

  3. [转] iOS 在UILabel显示不同的字体和颜色

    在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串,在iOS 6 以后我们可以很轻松的实现这一点,官方的API 为我们提供了UILabel类的at ...

  4. iOS 在UILabel显示不同的字体和颜色(ios6 and later)

    在项目开发中,我们经常会遇到在这样一种情形:在一个UILabel 使用不同的颜色或不同的字体来体现字符串,在iOS 6 以后我们可以很轻松的实现这一点,官方的API 为我们提供了UILabel类的at ...

  5. 李洪强iOS开发之-修改状态栏的字体的颜色

    李洪强iOS开发之-修改状态栏的字体的颜色 修改的效果: -(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; [ ...

  6. iOS UILabel换行同时修改字体大小颜色

    UIButton *onlyPriceBtn = [UIButton buttonWithType:UIButtonTypeCustom]; onlyPriceBtn.layer.borderColo ...

  7. 给UILabel设置不同的字体和颜色

    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAt ...

  8. iOS开发-- 设置UIButton的文字显示位置、字体的大小、字体的颜色

    btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @"search" forState: UIControlS ...

  9. iOS关于UILabel 基本属性 背景图片 背景色

    [代码] iOS关于UILabel 基本属性 背景图片 背景色 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ...

随机推荐

  1. linux系统灵活运用灯[android课程3]

    1,文件如何生成: ----- ---- (二),把hello例子贴过来后,编译问题: 在编译Android 4.0驱动的时候,使用到了proc_dir_entry结构体中的owner成员,但是编译的 ...

  2. 1890. Money out of Thin Air(线段树 dfs转换区间)

    1890 将树的每个节点都转换为区间的形式 然后再利用线段树对结点更新 这题用了延迟标记 相对普通线段树 多了dfs的转换 把所要求的转换为某段区间 RE了N次 最后没办法了 记得有个加栈的语句 拿来 ...

  3. POJ 1552 Doubles (C++ STL set使用)

    题目: 题意:题意:给出几个正数(2~15个),然后就是求有这些数字的2倍有没有和原先的正数相同的,求出有几个,没有就是0. 分析:水题.用数组解决,开一个数组存正数,另开一个数组用来存这些数的2倍, ...

  4. MySql命令的基本操作

    MySQL的基本命令: 登录:mysql  -h -u root -p password:*****; 例如:mysql -h127.0.0.1 -uroot -p 增加一个用户: grant all ...

  5. UVa 129 (回溯法) Krypton Factor

    回溯法确实不是很好理解掌握的,学习紫书的代码细细体会. #include <cstdio> ]; int n, L, cnt; int dfs(int cur) { if(cnt++ == ...

  6. 解决java switch……case不能匹配字符串的问题

    java1.7已经支持了匹配字符串 方案1. enum Animal { dog,cat,bear; public static Animal getAnimal(String animal){ re ...

  7. 在ASP.NET中各种跳转控制

    在ASP.NET中各种跳转控制 分类: 我的资料2012-03-16 15:01 76人阅读 评论(0) 收藏 举报 asp.netjavascripturlmenu Respose.Write(&q ...

  8. create a C# context menu from code

    I try the one of your approach, it works well in my computer. Below is my code: public void AddConte ...

  9. 7、四大组件之二-Service高级

    一.Native Service 1>什么是Native Service 使用JNI编写,在系统启动完成之前启动的系统级服务. 2>哪些服务是Native Service ACCESSIB ...

  10. 动态代理CGlib实例

    1.委托类: package 动态代理2; //需要对这个类进行增强 public class UserService { public void create() { System.out.prin ...