突出显示给定字符串中所有字符的字符串中的特定字符

时间:2014-03-14 08:46:33

标签: ios iphone objective-c nsattributedstring processing-efficiency

特殊字符在标签上用红色突出显示,所以我写下面的函数效果很好,但我想确认,还有其他有效的方法吗?例如

-(NSMutableAttributedString*)getAttributeText:(NSString*)string forSubstring:(NSString*)searchstring {
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_lblName.text];
    NSRange searchRange = NSMakeRange(0,string.length);
    for (NSInteger charIdx=0; charIdx<searchstring.length; charIdx++){
        NSString *substring = [searchstring substringWithRange:NSMakeRange(charIdx, 1)];
        NSRange foundRange;
        searchRange.location = 0;
        while (searchRange.location < string.length) {
            searchRange.length = string.length-searchRange.location;
            foundRange = [string rangeOfString:substring options:1 range:searchRange];
            [text addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range:foundRange];
            if (foundRange.location != NSNotFound) {
                searchRange.location = foundRange.location+foundRange.length;
            } else {
                // no more substring to find
                break;
            }
        }
    }
    return text;
}

以下是我如何使用它的代码,以及结果

NSString *string = @"James Bond Always Rocks";
_lblName.text = string;
_lblAttributedName.attributedText = [self getAttributeText:string forSubstring:@"ao"];

enter image description here

更新

NSString *string = @"James Bond Always Rocks";    
NSRange range = [string rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"J"] options:NSCaseInsensitiveSearch];
NSLog(@"range->%@",NSStringFromRange(range)); //This prints range->{0, 1}

NSString *string = @"James Bond Always Rocks";    
NSRange range = [string rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"j"] options:NSCaseInsensitiveSearch];
NSLog(@"range->%@",NSStringFromRange(range)); //This prints range->{2147483647, 0}

3 个答案:

答案 0 :(得分:5)

您可以通过搜索模式(在您的示例中为“[ao] +”)来简化它以消除 外循环:

NSString *string = @"James Bond Always Rocks";
NSString *searchstring = @"ao";
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];

// A regular expression pattern that matches a sequence of the characters in "searchString":
NSString *pattern = [NSString stringWithFormat:@"[%@]+", [NSRegularExpression escapedPatternForString:searchstring]];

NSRange foundRange = [string rangeOfString:pattern options:NSRegularExpressionSearch|NSCaseInsensitiveSearch];
while (foundRange.location != NSNotFound) {
    [text addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range:foundRange];
    NSRange nextRange = NSMakeRange(foundRange.location + foundRange.length, string.length - foundRange.location - foundRange.length);
    foundRange = [string rangeOfString:pattern options:NSRegularExpressionSearch|NSCaseInsensitiveSearch range:nextRange];
}

答案 1 :(得分:4)

这是我的版本,这是使用简单循环的非常基本的方法。

为什么我发布它是因为我跟踪了效率,请查看每个实施所花费的时间。

2014-03-14 15:48:42.792 TimeEfficiency [1166:303]我:0.000073秒

2014-03-14 15:48:45.319 TimeEfficiency [1166:303] martin:0.000278秒

2014-03-14 15:48:48.263 TimeEfficiency [1166:303] avt:0.000029秒

2014-03-14 15:48:51.152 TimeEfficiency [1166:303] janak:0.000092秒

因此,Avt的时间表现最佳。


NSString *string = @"James Bond Always Rocks";
NSString *searchstring = @"ao";

NSMutableArray *characters = [NSMutableArray new];
for (NSInteger i=0; i<searchstring.length; i++) {
    [characters addObject:[searchstring substringWithRange:NSMakeRange(i, 1)]]; //ao
}
//store all the location of each of the char
NSMutableArray *locations = [NSMutableArray new];
for (NSInteger i=0; i<string.length; i++) {
    if ([characters containsObject: [string substringWithRange:NSMakeRange(i, 1)]] ){
        [locations addObject:@(i)];
    }
}

//loop for string and for each location change the color

NSMutableAttributedString *text = [[NSMutableAttributedString alloc]initWithString:string];
for (NSInteger i=0; i<locations.count; i++) {
    NSRange range=NSMakeRange([locations[i] intValue], 1);
    [text addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
}

答案 2 :(得分:2)

我的NSCharacterSet变体

- (NSMutableAttributedString*)getAttributeText:(NSString*)string forSubstring:(NSString*)searchstring {
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:string];
    NSRange searchRange = NSMakeRange(0,string.length);
    NSString *allCaseString = [[searchstring uppercaseString] stringByAppendingString:[searchstring lowercaseString]];
    NSCharacterSet *chSet = [NSCharacterSet characterSetWithCharactersInString:allCaseString];
    NSRange foundRange;

    while (foundRange = [string rangeOfCharacterFromSet:chSet options:NSCaseInsensitiveSearch range:searchRange],
           foundRange.location != NSNotFound) {
        [text addAttribute: NSForegroundColorAttributeName value: [UIColor redColor] range:foundRange];
        NSUInteger newStart = foundRange.location + foundRange.length;
        searchRange = (NSRange){newStart, string.length - newStart};
    }
    return text;
}