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

iOS 同一UILabel改变数字的颜色

时间:2015-11-20 12:38:03      阅读:566      评论:0      收藏:0      [点我收藏+]

标签:

 
- (void)setRichNumberWithLabel:(UILabel *)label Color:(UIColor *) color FontSize:(CGFloat)size{
//将Label的text转化为NSMutalbeAttributedString
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:label.text];
//定义空字符串
    NSString *temp = nil;
//根据属属性字符串的长度循环
    for(int i = 0; i < [attributedString length]; i++){
//每次取1个长度的字符串
        temp = [label.text substringWithRange:NSMakeRange(i, 1)];
//判读这个长度的字符串中是否包含数字以及标点符号
        if([temp isEqualToString:@"0"] || [temp isEqualToString:@"1"] || [temp isEqualToString:@"2"] || [temp isEqualToString:@"3"] || [temp isEqualToString:@"4"] || [temp isEqualToString:@"5"] || [temp isEqualToString:@"6"] || [temp isEqualToString:@"7"] || [temp isEqualToString:@"8"] || [temp isEqualToString:@"9"]|| [temp isEqualToString:@"."] || [temp isEqualToString:@"-"]){
//给符合条件的属性字符串添加颜色,字体
[attributedString setAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                 color, 
                    NSForegroundColorAttributeName, [UIFont fontWithName:FONT_LANTING_JIANHEI size:size],
                    NSFontAttributeName, nil] range:NSMakeRange(i,
1)]; } } //重新给Label的text传递处理好的属性字符串 label.attributedText = attributedString; }

 

iOS 同一UILabel改变数字的颜色

标签:

原文地址:http://www.cnblogs.com/MrFeng/p/4980208.html

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