
示意圖1

示意圖2
1. 給NSString分類添加搜索字段范圍檢索的方法
思路:將一個
String通過searchText分割成一個數(shù)組,然后通過subString和searchText的長度和來獲取需要檢索的Range核心代碼:
/** 遍歷self中的searchText字段,通過block返回對應的range和索引
* @param searchText 搜索(高亮)字段
* @param block 檢索到對象的回調, idx : 遍歷對象的索引(從0開始),range :遍歷對象所在范圍。如果沒有匹配到相關字段,block將不會被調用
*/
- (void)enumerateSearchText:(NSString *)searchText
usingBlock:(void (NS_NOESCAPE ^)(NSUInteger idx, NSRange range, BOOL *stop))block {
if (!self.length || !searchText.length || ![self containsString:searchText]) return;
NSUInteger __block len = searchText.length;
NSUInteger __block loc = 0;
NSArray <NSString *>*subStrings = [self componentsSeparatedByString:searchText];
[subStrings enumerateObjectsUsingBlock:^(NSString *subText, NSUInteger idx, BOOL *stop) {
loc += subText.length; // 當前idx對應searchText的location
NSRange range = NSMakeRange(loc, len);
if (block) block(idx, range, stop);
loc += len; // 下一個subText的location
if (idx == subStrings.count - 2) *stop = YES; // 到最后一個截止(不包括最后一個)
}];
}
2. 給NSString分類添加高亮顯示搜索字段的方法
/** 自動匹配搜索字段,將self生成富文本輸出。 如果self == nil, 則返回 nil
* @param searchText 搜索(高亮)字段
* @param textColor 默認字體顏色
* @param textFont 默認字體
* @param searchTextColor 高亮文本顏色
* @param searchTextFont 高亮文本字體。
*/
- (NSAttributedString *)attributedTextWithSearchText:(NSString *)searchText
textColor:(UIColor *)textColor
textFont:(UIFont *)textFont
searchTextColor:(UIColor *)searchTextColor
searchTextFont:(UIFont *)searchTextFont {
if(!self) return nil;
NSDictionary *textAttbs = @{
NSFontAttributeName: textFont,
NSForegroundColorAttributeName: textColor
};
NSMutableAttributedString *attbText = [[NSMutableAttributedString alloc] initWithString:self attributes:textAttbs];
if (!self.length || !searchText.length || ![self containsString:searchText])
return attbText;
NSDictionary __block *highlightAttbs = @{
NSFontAttributeName: searchTextFont,
NSForegroundColorAttributeName: searchTextColor
};
[self enumerateSearchText:searchText usingBlock:^(NSUInteger idx, NSRange range, BOOL *stop) {
[attbText setAttributes:highlightAttbs range:range];
}];
return attbText;
}
3. 進一步對UILabel(分類)進行拓展(可以省略)
- (void)setText:(nullable NSString *)text
searchText:(nullable NSString *)searchText
textColor:(nonnull UIColor *)textColor
textFont:(nonnull UIFont *)textFont
searchTextColor:(nonnull UIColor *)searchTextColor
searchTextFont:(nonnull UIFont *)searchTextFont {
self.attributedText = [text attributedTextWithSearchText:searchText
textColor:textColor
textFont:textFont
searchTextColor:searchTextColor
searchTextFont:searchTextFont];
}
4. 使用(在cell 中使用)
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.textLabel.numberOfLines = 0;
}
NSString *text = self.searchResults[indexPath.row];
[cell.textLabel setText:text
searchText:_searchBar.text
textColor:[UIColor colorWithWhite:0.03 alpha:1]
textFont:[UIFont systemFontOfSize:14]
searchTextColor:[UIColor colorWithRed:239/255.0 green:75/255.0 blue:76/255.0 alpha:1]
searchTextFont:[UIFont boldSystemFontOfSize:18]];
return cell;
}