码迷,mamicode.com
首页 > 移动开发 > 详细

iOS Label文字分段颜色

时间:2016-06-27 09:03:52      阅读:248      评论:0      收藏:0      [点我收藏+]

标签:

转自 http://blog.csdn.net/lwjok2007/article/details/48241285

iOS开发中经常会遇到这样的问题就是一个Label 中的文字需要两种以上的颜色现实

比如注册按钮下一般会有服务协议,这里的文字一般都会做区分

如下图

技术分享

 

我们直接看代码

 

[objc] view plain copy
 
  1. UILabel *hintLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 120, 320, 66)];  
  2.     hintLabel.numberOfLines=0;  
  3.     [self.view addSubview:hintLabel];  
  4.       
  5.     NSMutableAttributedString *hintString=[[NSMutableAttributedString alloc]initWithString:@"点击注册按钮即表示您已同意西游隐私条款和服务协议"];  
  6.     //获取要调整颜色的文字位置,调整颜色  
  7.     NSRange range1=[[hintString string]rangeOfString:@"西游隐私条款"];  
  8.     [hintString addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:range1];  
  9.       
  10.     NSRange range2=[[hintString string]rangeOfString:@"服务协议"];  
  11.     [hintString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range2];  
  12.       
  13.     hintLabel.attributedText=hintString;  


其实,就是使用到了NSMutableAttributedString   的功能,Label设置 attributedText就好了

iOS Label文字分段颜色

标签:

原文地址:http://www.cnblogs.com/LGLblog/p/5619138.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!