搜索字符串中字符出现的次数,并且高亮显示

时间:2020-11-27 19:02:20
objective-c :搜索字符串中字符出现的次数,并且高亮显示


- (NSMutableAttributedString *)colorData:(NSString *)withStr{
    NSMutableAttributedString *dataStr = [[NSMutableAttributedString alloc] initWithString:withStr];
    
    for (int i = 0; i < withStr.length - appDelegate.searchKeyWords.length + 1; i++) {
        
        if ([[withStr substringWithRange:NSMakeRange(i, appDelegate.searchKeyWords.length)] isEqualToString:appDelegate.searchKeyWords]) {
            
            NSRange range = NSMakeRange(i, appDelegate.searchKeyWords.length);
            [dataStr addAttribute:NSForegroundColorAttributeName value:SETCOLOR(255, 114, 0) range:NSMakeRange(range.location,range.length)];
            
        }
        
        
    }
    return dataStr;