iOS 字符中让数字变色

//下面是要变色的字符串  需要NSMutableAttributedString字体

NSMutableAttributedString*AttributedStr = [[NSMutableAttributedStringalloc]initWithString:@"已报名02人/限额200人"];

//下面是要变色的字符串

NSString*nsstring = [NSStringstringWithFormat:@"%@",AttributedStr];

// 取出字符串长度做循环

for(int i =0; i < [nsstring length]; ++i) {

// 取出第几位

int a = [nsstring characterAtIndex:i];

//判断是否为数字

if(isdigit(a)){

//是

NSLog(@"%d",i);

就让第i位变色

[AttributedStraddAttribute:NSFontAttributeName

value:[UIFontsystemFontOfSize:16.0]

range:NSMakeRange(i,1)];

[AttributedStraddAttribute:NSForegroundColorAttributeName

value:[UIColorblueColor]

range:NSMakeRange(i,1)];

}else{

NSLog(@"%d",i);


}

}

你可能感兴趣的:(iOS 字符中让数字变色)