【发布时间】: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;
}
【问题讨论】:
标签: ios objective-c search highlight case-insensitive