NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:@"Using NSAttributed String,try your best to test attributed string text"];

[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" size:30.0] range:NSMakeRange(0, 5)];

[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:30.0] range:NSMakeRange(6, 12)];

[str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial" size:30.0] range:NSMakeRange(19, 6)];

UILabel *attrLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 150, 320 - 40, 90)];

attrLabel.attributedText = str;

attrLabel.numberOfLines = 0;

[self.view addSubview:attrLabel];

NSString *keyword = @"开源";
NSString *result = @"开源中国社区";
 
// 设置标签文字
NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:result];
 
// 获取标红的位置和长度
NSRange range = [result rangeOfString:keyword];
 
// 设置标签文字的属性
[attrituteString setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],   NSFontAttributeName : [UIFont systemFontOfSize:17]} range:range];
 
// 显示在Label上
label.attributedText = attrituteString;

给UILabel设置不同的字体和颜色的更多相关文章

  1. QT5如何设置QLabel中字体的颜色

    修改了wd的文章: 如何使用Qt5,设置QLabel中字体的颜色. 大致有几种做法: 一是使用setPalette()方法: 二是使用样式表: 三是可以使用QStyle: 四是可以在其中使用一些简单的 ...

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

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

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

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

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

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

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

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

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

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

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

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

  8. aspxGridview 根据单元格值得不同,设置单元格字体的颜色(设置和读取值)

    protected void ASPxGridView1_HtmlRowCreated(object sender,DevExpress.Web.ASPxGridView.ASPxGridViewTa ...

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

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

随机推荐

  1. vb.net dll创建

    创建vb.net的动态链接库 如果你想用用VC来编写vb.net的dll,我想本文不适合. 本文只说vb.net的dll. 何为vb.net的dll?实际上就是一个类库. 很多个类封装成一个库了,这就 ...

  2. Delphi结构体的扩展,可以自动初始化,反初始化,自定义拷贝函数.

    转载:http://www.raysoftware.cn/?p=518&utm_source=tuicool 恭贺Delphi XE7诞生,Delphi XE7在编译器内部集成了我之前所实现的 ...

  3. Python 学习笔记二

    笔记二 :print 以及基本文件操作 笔记一已取消置顶链接地址 http://www.cnblogs.com/dzzy/p/5140899.html 暑假只是快速过了一遍python ,现在起开始仔 ...

  4. 18.4---2出现了几次(CC150)

    思路:1,先给出LTE的代码: public static int countNumberOf2s(int n) { // write code here int res = 0; for(int i ...

  5. asp.net mvc 入门资料

    七天学会ASP.NET MVC (一)——深入理解ASP.NET MVC http://www.cnblogs.com/powertoolsteam/p/MVC_one.html 无废话MVC入门教程 ...

  6. OC block的简单使用

    http://blog.csdn.net/itpeng523/article/details/23965147 一.先用Xcode创建一个空工程 学习block之前先用弄懂c语言的函数指针 看代码: ...

  7. javascript 事件委托,jq,js模拟事件

    <!DOCTYPE> <html> <head> <title></title> <script src="Scripts/ ...

  8. ffmpeg-20160325-snapshot-static-bin

    ffmpeg-20160325-snapshot-static.7z ./configure \ --enable-static \ --disable-shared \ --enable-gpl \ ...

  9. vs2013显示行号

    随便打开一个项目,可以看到代码框内并没有显示行号 选择“工具”-“选项”,打开后界面如下 选择文本编辑器,找到下图中的“行号”并勾选 行号可以显示了 5 这样我们就完成了任务

  10. EL表达式中fn函数 (转载)

    JSTL 使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请求参数或者context以及 session中的数据的访问非常方便,但是我们在实际应用中经常需 ...