zoukankan      html  css  js  c++  java
  • 开发中讲字符串中的数字改变颜色

     1  
     2     NSString * str =@"您一共购买2件商品,消费总额为:196";
     3     // 搜索文字
     4     NSString *tempStr = @"\d{1,}";
     5     
     6     NSMutableArray *results = [NSMutableArray array];
     7     NSRange searchRange = NSMakeRange(0, [str length]);
     8     NSRange range;
     9     NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:str];
    10     while ((range = [str rangeOfString:tempStr options:NSRegularExpressionSearch range:searchRange]).location != NSNotFound)
    11     {
    12         [results addObject:[NSValue valueWithRange:range]];
    13         searchRange = NSMakeRange(NSMaxRange(range), [str length] - NSMaxRange(range));
    14         [string addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
    15     }
    16      summary.attributedText = string;

    效果如下图:

  • 相关阅读:
    元素定位方法与等待
    xpath定位的总结
    模拟登录
    shell的数组
    shell的函数
    shell的循环
    shell的流程控制语句case
    shell的while循环
    shell的if条件判断
    shell的for循环
  • 原文地址:https://www.cnblogs.com/angongIT/p/4180240.html
Copyright © 2011-2022 走看看