iOS富文本中第一个数字到最后一个数字之间的内容改色改字体

13 篇文章 0 订阅

本文可以找个一个label 中的数字和其他类型混排中的数字

- (NSMutableAttributedString *)getPhoneFromData:(NSString *)data{

    
    NSString *phone = data;
    NSString *temp1 =[[NSString alloc]init];
    NSString *strNew = [[NSString alloc]init];
    NSMutableArray *arrr = [[NSMutableArray alloc]init];
    for (int i = 0; i < phone.length; i++) {
        temp1 =[phone substringWithRange:NSMakeRange(i, 1)];
        if ([temp1 isEqualToString:@"0"]||
            [temp1 isEqualToString:@"1"]||
            [temp1 isEqualToString:@"2"]||
            [temp1 isEqualToString:@"3"]||
            [temp1 isEqualToString:@"4"]||
            [temp1 isEqualToString:@"5"]||
            [temp1 isEqualToString:@"6"]||
            [temp1 isEqualToString:@"7"]||
            [temp1 isEqualToString:@"8"]||
            [temp1 isEqualToString:@"9"]) {
            NSString *str = [NSString stringWithFormat:@"%d",i];
            
            [arrr addObject:str];
        };
        
    }
    NSLog(@"%@",phone);
    NSLog(@"记录数字位数%@",arrr);
    NSString *kaishi =[[NSString alloc]init];
    NSString *jieshu = [[NSString alloc]init];
    
    kaishi = [NSString stringWithFormat:@"%@",arrr[0]];
    
    NSUInteger int1 = arrr.count;
    jieshu = [NSString stringWithFormat:@"%@",arrr[int1-1]];
    NSLog(@"%@-%@",kaishi,jieshu);
    int ks=[kaishi intValue];
    int js =[jieshu intValue];
    int zs = js - ks ;
    NSLog(@"%d",zs);
    UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 60 , 320, 30)];
    
    testLabel.backgroundColor = [UIColor lightGrayColor];
    
    testLabel.textAlignment = NSTextAlignmentCenter;
    
    
    [self.view addSubview:testLabel];
    
    NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:phone];
    
    [AttributedStr addAttribute:NSFontAttributeName
     
                          value:[UIFont systemFontOfSize:16.0]
     
                          range:NSMakeRange(ks+1, zs+1)];
    
    [AttributedStr addAttribute:NSForegroundColorAttributeName
     
                          value:[UIColor redColor]
     
                          range:NSMakeRange(ks, zs+1)];

    return AttributedStr;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值