【问题标题】:highlight search result with case insensitive突出显示不区分大小写的搜索结果
【发布时间】:2018-02-28 23:15:54
【问题描述】:

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

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

如何使它在突出显示文本时不区分大小写?

谢谢!

- (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

【问题讨论】:

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


    【解决方案1】:

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

    尝试以下方法:

    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;
    }
    

    【讨论】:

    • 哇 rmaddy 救救我!谢谢!
    • 很高兴为您提供帮助。请不要忘记通过单击答案左侧的复选标记来接受有用的答案。这让人们知道问题已经解决。
    • 好的!我做到了! :-D 谢谢~~
    猜你喜欢
    • 2018-04-26
    • 2011-02-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-13
    • 2010-09-15
    • 1970-01-01
    • 2011-03-18
    相关资源
    最近更新 更多