需求:在一个字符串 ASting中或许存在多个 abc字符串,将所有的abc字符串高亮显示。
解决方案:先遍历ASting找到所有的abc所在的位置的index,然后通过index将字符串进行替换
相关代码:
#pragma mark - 获取这个字符串ASting中的所有abc的所在的index
- (NSMutableArray *)getRangeStr:(NSString *)text findText:(NSString *)findText
{
NSMutableArray *arrayRanges = [NSMutableArray arrayWithCapacity:3];
if (findText == nil && [findText isEqualToString:@""])
{
return nil;
}
NSRange rang = [text rangeOfString:findText]; //获取第一次出现的range
if (rang.location != NSNotFound && rang.length != 0)
{
[arrayRanges addObject:[NSNumber numberWithInteger:rang.location]];//将第一次的加入到数组中
NSRange rang1 = {0,0};
NSInteger location = 0;
NSInteger length = 0;
for (int i = 0;; i++)
{
if (0 == i)
{
//去掉这个abc字符串
location = rang.location + rang.length;
length = text.length - rang.location - rang.length;
rang1 = NSMakeRange(location, length);
}
else
{
location = rang1.location + rang1.length;
length = text.length - rang1.location - rang1.length;
rang1 = NSMakeRange(location, length);
}
//在一个range范围内查找另一个字符串的range
rang1 = [text rangeOfString:findText options:NSCaseInsensitiveSearch range:rang1];
if (rang1.location == NSNotFound && rang1.length == 0)
{
break;
}
else//添加符合条件的location进数组
[arrayRanges addObject:[NSNumber numberWithInteger:rang1.location]];
}
return arrayRanges;
}
return nil;
}
网友评论