• iOS 同一UILabel改变数字的颜色


     
    - (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; }
  • 相关阅读:
    javascript 调试代码
    简洁的js拖拽代码
    搭个小窝
    FastDFS随笔
    JDK6和JDK7中String的substring()方法及其差异
    杂笔
    JVM内存随笔
    java中的final和volatile详解
    关于java的Synchronized,你可能需要知道这些(下)
    关于java的Synchronized,你可能需要知道这些(上)
  • 原文地址:https://www.cnblogs.com/MrFeng/p/4980208.html
Copyright © 2020-2023  润新知