美文网首页
ios UILabel指定文字变色

ios UILabel指定文字变色

作者: 缘來諟夢 | 来源:发表于2021-08-31 10:09 被阅读0次
    NSString * noticeStr = [NSString stringWithFormat:@"将获得¥%@",[YZHttpmanager positiveFormat:amountStr]];
        NSArray *number = @[@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@".",@",",@"¥"];
        NSMutableAttributedString *attributeString  = [[NSMutableAttributedString alloc]initWithString:noticeStr];
        for (int i = 0; i < noticeStr.length; i ++) {
            //这里的小技巧,每次只截取一个字符的范围
            NSString *a = [noticeStr substringWithRange:NSMakeRange(i, 1)];
            //判断装有0-9的字符串的数字数组是否包含截取字符串出来的单个字符,从而筛选出符合要求的数字字符的范围NSMakeRange
            if ([number containsObject:a]) {
                [attributeString setAttributes:@{NSForegroundColorAttributeName:UIColorFromRGB(0xFF5A00),NSFontAttributeName:[UIFont boldSystemFontOfSize:25],NSUnderlineStyleAttributeName:[NSNumber numberWithInteger:0]} range:NSMakeRange(i, 1)];
            }
        }
        amountlab.attributedText=attributeString;
    

    相关文章

      网友评论

          本文标题:ios UILabel指定文字变色

          本文链接:https://www.haomeiwen.com/subject/ldtziltx.html