搜索匹配关键字
//改变关键字颜色
//调用在你的cell中 tableview代理 cellforrow
//返回单元格内容
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *str = @"ID";
WHXSearchCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
if (cell == nil) {
cell = [[WHXSearchCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}
if (_isarray==YES) {
SearchModel *mode = _dataArray[indexPath.row];
cell.index = NO;
cell.model = mode;
}else{
SearchModel *mode = _searchArray[indexPath.row];
if (_searchArray.count==1) {
cell.index = YES;
}
cell.delegat = self;
[cell setModel:mode];
//搜索匹配关键字
// [cell.namelabel setAttributedText:[self attrStrFrom:mode.labelName searchStr:_TextField.text]];
[cell.namelabel setAttributedText:[self rangeSearchLight:mode.labelName searchString:_TextField.text]];
}
return cell;
}
//改变状态
- (NSMutableAttributedString *)rangeSearchLight:(NSString *)string searchString:(NSString *)searchString
{
NSString *strAfterDecodeByUTF8AndURI = [searchString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSRange range = [string rangeOfString:strAfterDecodeByUTF8AndURI];
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc]initWithString:string];
[attr setAttributes:@{NSForegroundColorAttributeName:QBCOLOR_RGB(87, 139, 237)}
range:range];
return attr;
}
//改变字体和颜色方法
//- (NSMutableAttributedString *)attrStrFrom:(NSString *)titleStr searchStr:(NSString *)searchStr
//{
// NSMutableAttributedString *arrString = [[NSMutableAttributedString alloc]initWithString:titleStr];
// // 设置前面几个字串的格式:粗体、红色
// [arrString addAttributes:@{NSFontAttributeName:[UIFont fontWithName:@"Helvetica-Bold" size:18.0],
// NSForegroundColorAttributeName:QBCOLOR_RGB(87, 139, 237)
// }
// range:[titleStr rangeOfString:searchStr]];
// return arrString;
//}//