self.labelContent.attributedText = [self highlightText:self.searchStr inStr:searchModel.eventDesc];

- (NSAttributedString *)highlightText:(NSString *)keyword inStr:(NSString *)str {
NSRange range = [str rangeOfString:keyword];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:str];
if (range.length > ) {
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range];
} else {
}
return [[NSAttributedString alloc]initWithAttributedString:attrStr];
}
- (NSAttributedString *)highlightText:(NSString *)keyword inStr:(NSString *)str {
NSRange range = [str rangeOfString:keyword];
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc]initWithString:str];
if (range.length > ) {
[attrStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:range]; } else { } return [[NSAttributedString alloc]initWithAttributedString:attrStr];
} #pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *str = [self.strs objectAtIndex:indexPath.row];
cell.textLabel.attributedText = [self highlightText:self.searchTextField.text inStr:str];
}

iOS - 搜索关键字在结果中高亮显示的更多相关文章

  1. 【Vue.js】基于vue的实时搜索,在结果中高亮显示关键词

    一.搜素效果如下: 二.核心 1)利用oninput属性来触发搜素功能 2)利用RegExp来对字符串来全局匹配关键字,利用replace方法来对匹配的关键字进行嵌入高亮的<span class ...

  2. Asp.net 中高亮显示搜索关键字简单方法

    今天用到搜索时的高亮显示,百度了一下,如下面: 1.替换关键字,对字体变色.         public static string ReplaceRed(string strtitle, stri ...

  3. 在 Angular 中实现搜索关键字高亮

    在 Angular 中,我们不应该试图直接修改 DOM 的内容,当需要更新 DOM 内容的时候,应该修改的其实是我们的数据模型,也就是 $scope 中的数据,Angular 会帮助我们将修改之后的数 ...

  4. 如何给wp(Windows phone)中搜索关键字加亮?

    问题来源 最近在群里看到群友讨论在wp中有个搜索功能,要求搜索关键字在搜索结果内容中加亮(即加颜色),由于wp中没有自带这样的控件,于是大家各抒自见,有人说用第三方控件,有人说用richtextbox ...

  5. spring data solr 搜索关键字高亮显示

    spring data solr 搜索关键字高亮显示 public Map<String, Object> highSearch(Map searchMap) { Map map = ne ...

  6. react项目中实现搜索关键字呈现高亮状态(一)

    最近有个需求,在一个react项目中,实现搜索关键字呈现高亮状态.这个在普通的html文件中还好操作些,在react项目中有点懵逼了,因为react项目中很少操作dom,有点无从下手.但最后还是实现了 ...

  7. Java操作Excel(读、写、搜索关键字、插入图片)

    import java.io.File; import java.io.IOException; import jxl.Cell; import jxl.Sheet; import jxl.Workb ...

  8. 仿百度壁纸客户端(五)——实现搜索动画GestureDetector手势识别,动态更新搜索关键字

    仿百度壁纸客户端(五)--实现搜索动画GestureDetector手势识别,动态更新搜索关键字 百度壁纸系列 仿百度壁纸客户端(一)--主框架搭建,自定义Tab + ViewPager + Frag ...

  9. 织梦dedecms后台文章搜索关键字,关键字包含文章内容的代码修改

    1.织梦dedecms后台文章搜索功能在哪里找?织梦dedecms后台-->核心-->常用操作-->所有档案列表(或)织梦dedecms后台-->核心-->内容管理--& ...

随机推荐

  1. ansible(三)

    setup ansible_all_ipv4_addresses # ipv4的所有地址 ansible_all_ipv6_addresses # ipv6的所有地址 ansible_date_tim ...

  2. FFLIB

    用于分布式程序的c++类库,封装了socket.rpc.lua.CQRS框架.算法等组件,适于SNS.WEBGAME.MMO后台程序, about C++,linux https://github.c ...

  3. 用BlockBoundQueue和c++11实现多线程生产者消费者问题

    // file : blockBoundQueue.h #ifndef YANG_BLOCKBOUNDQUEUE #define YANG_BLOCKBOUNDQUEUE #include <m ...

  4. python应用-给出行数,输出相应的杨辉三角

    def main(): num = int(input('Number of rows: ')) yh = [[]] * num for row in range(num): yh[row] = [N ...

  5. CF300D Painting Square

    Painting Square https://codeforces.com/problemset/problem/300/D 给了一个理解起来较复杂但是本质上很简单的分形. 题解 很显然,只有边长为 ...

  6. Mac下用java代码调用adb命令时出错

    原本我直接这样写: Process process=Runtime.getRuntime().exec("adb devices"); 但是运行时出错: java.io.IOExc ...

  7. spark——故障排除

    故障排除一:控制reduce端缓冲大小以避免OOM 在Shuffle过程,reduce端task并不是等到map端task将其数据全部写入磁盘后再去拉取,而是map端写一点数据,reduce端task ...

  8. django-评论

    视图函数views.py # 订单评论 class OrderCommentView(View): def get(self, request, order_id): # 获取用户信息 user = ...

  9. 什么是JavaEE,什么是Spring

    作者:大宽宽链接:https://www.zhihu.com/question/268742981/answer/341770209来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...

  10. Increasing Performance by Reducing Dynamic Dispatch

    https://developer.apple.com/swift/blog/?id=39 Increasing Performance by Reducing Dynamic Dispatch Li ...