需求:要给输入的长度小于8,大于18的数字标红显示,用自带的rangeOfString方法,经常会找到前面的range,导致显示异常. 所以自己计算出range
NSArray *arr = [operatedStrM componentsSeparatedByString:@","];
int i = 0;
for (NSString *str in arr) {
NSInteger length = str.length;
if (length) {
if (!(length >= 8 && length <= 18)) {
self.redCount++;
int j = i;
if (j > 0) {
for (int k = 0; k < textLength; k++) {
NSString *subStr = [operatedStrM substringAtRange:NSMakeRange(k, 1)];
if ([subStr isEqualToString:@","]) {
j--;
if (j == 0) {
j = k + 1;
break;
}
}
}
}
[attributeStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(j, length)];
}
}
i++;
}
网友评论