突出显示不区分大小写的搜索结果

时间:2017-09-20 03:31:06

标签: ios objective-c search highlight case-insensitive

我制作了搜索栏和表格视图。

列出的搜索结果不区分大小写。我添加了一些代码来突出显示单元格文本中的搜索关键字。但关键词突出了区分大小写。

如何在高亮文本中使其不区分大小写?

谢谢!

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"CityCell";
CityTableViewCell *cell = (CityTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {
    NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CityTableCell" owner:self options:nil];
    cell = [nib objectAtIndex:0];
}


if (searchEnabled) {

    cell.cityLabel.text = [_filterdCityArray objectAtIndex:indexPath.row];
    cell.stateLabel.text = [_filterdStateArray objectAtIndex:indexPath.row];
    cell.urlLabel.text = [_filterdUrlArray objectAtIndex:indexPath.row];

    NSInteger srcTxtLen = (int)_searchKey.length;
    NSInteger idxCity = 0;
    NSInteger idxState = 0;

    while (idxCity<(cell.cityLabel.text.length-srcTxtLen)) {
        NSRange srcRange = NSMakeRange(idxCity, srcTxtLen);
        if ([[cell.cityLabel.text substringWithRange:srcRange] isEqualToString:_searchKey]) {
            NSMutableAttributedString *tmpAttrTxt = [[NSMutableAttributedString alloc] initWithAttributedString:cell.cityLabel.attributedText];
            [tmpAttrTxt addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:srcRange];
            cell.cityLabel.attributedText = tmpAttrTxt;
            idxCity += srcTxtLen;
        }
        else {
            idxCity++;
        }
    }

    while (idxState<(cell.stateLabel.text.length-srcTxtLen)) {
        NSRange srcRange = NSMakeRange(idxState, srcTxtLen);
        if ([[cell.stateLabel.text substringWithRange:srcRange] isEqualToString:_searchKey]) {
            NSMutableAttributedString *tmpAttrTxt = [[NSMutableAttributedString alloc] initWithAttributedString:cell.stateLabel.attributedText];
            [tmpAttrTxt addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:srcRange];
            cell.stateLabel.attributedText = tmpAttrTxt;
            idxState += srcTxtLen;
        }
        else {
            idxState++;
        }
    }


}
else{
    ..........
}


return cell;
}

in simulator

1 个答案:

答案 0 :(得分:0)

您找到匹配的代码非常低效且过于复杂。

尝试以下方法:

NSString *text = cell.cityLabel.text;
NSRange *range = [text rangeOfString:_searchKey options: NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch];
if (range.location != NSNotFound) {
    NSMutableAttributedString *tmpAttrTxt = [[NSMutableAttributedString alloc] initWithAttributedString:text];
    [tmpAttrTxt addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:range];
}

你可以把它变成一个小帮手方法,让你的代码变得更好:

- (NSAttributedString *)highlightedText:(NSString *)text forSearch:(NSString *)searchText {
    NSMutableAttributedString *result = [[NSMutableAttributedString alloc] initWithAttributedString:text];
    NSRange *range = [text rangeOfString:searchText options: NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch];
    if (range.location != NSNotFound) {
        [result addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:range];
    }

    return result;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"CityCell";

    CityTableViewCell *cell = (CityTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"CityTableCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    if (searchEnabled) {
        NSString *city = _filterdCityArray[indexPath.row];
        NSString *state = _filterdStateArray[indexPath.row];
        NSString *url = _filterdUrlArray[indexPath.row];

        cell.cityLabel.attributedText = [self highlightedText:city forSearch:_searchKey];
        cell.stateLabel.attributedText = [self highlightedText:state forSearch:_searchKey];
        cell.urlLabel.text = url;
    }

    return cell;
}