美文网首页
iOS 关于字符串有相同字符的情况下 查找range的问题

iOS 关于字符串有相同字符的情况下 查找range的问题

作者: 差一点不是帅哥 | 来源:发表于2017-08-21 14:40 被阅读0次

前言

当我们项目中有要遍历一段字符串,找出需要高亮显示的文字的情况中,有时候这段字符会包含一样的字符,这时候就会出现问题了。
C44DDDE53C4CD6431F490E92D0821E17.jpg 5675533D-14C7-4A5C-8FD2-9AD375889A83 2.png

-(void)test{
//假数据,这里只用来测试,真实数据情况下就用model模型了,见谅!!
NSDictionary *dic1 = @{@"type":@"0",@"content":@"订单"};
NSDictionary *dic2 = @{@"type":@"3",@"content":@"测试跳转"};
NSDictionary *dic3 = @{@"type":@"0",@"content":@"还未反馈,广告主"};
NSDictionary *dic4 = @{@"type":@"8",@"content":@"差一点是帅哥"};
NSDictionary *dic5 = @{@"type":@"0",@"content":@"提醒你快去"};
NSDictionary *dic6 = @{@"type":@"9",@"content":@"反馈"};
NSDictionary *dic7 = @{@"type":@"0",@"content":@"哦!"};
// 添加到总得数据源数组
     NSMutableArray * array = [[NSMutableArray alloc]initWithObjects:dic1,dic2,dic3,dic4,dic5,dic6,dic7,nil];
// 拼接字符串
    NSMutableArray *strArr =[NSMutableArray arrayWithCapacity:0];
      for (NSDictionary *dicttion in array) {
     [strArr addObject:dicttion[@"content"]];
      &nbsp }
    _pinChuan3 = [strArr componentsJoinedByString:@""];
//设置attributed
NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_pinChuan3];
// 字体大小
text.font = [UIFont boldSystemFontOfSize:16.0f];
// 设置行间距
text.lineSpacing = 5;
__weak typeof(self) weakSelf = self;
__block NSString * content1;
//遍历查找字符串
[array enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    NSDictionary * dict = obj;
    if ([dict[@"type"] integerValue] !=0) {
    content1 = dict[@"content"];
    //找range
    NSRange range = [_pinChuan3 rangeOfString:content1];
    [text setTextHighlightRange:range color:[UIColor redColor] backgroundColor:[UIColor clearColor] userInfo:nil tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
     NSLog(@"点击");
     [weakSelf didSelectedDic:dict];//点击高度文字操作
   } longPressAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
     NSLog(@"长按");
     }];
    }
   }];
// Set to YYLabel or YYTextView.
    YYLabel *label = [YYLabel new];
    CGFloat Height = [self getterSpaceLabelHeight:_pinChuan3 withLineSpacing:5 withFont:[UIFont systemFontOfSize:16.0f] withWidth:KScreenWidth - 40];
    label.frame = CGRectMake(20, 300,KScreenWidth - 40, Height);
    label.attributedText = text;
    label.numberOfLines = 0;
    label.font = [UIFont systemFontOfSize:16.0f];
    label.backgroundColor = [UIColor lightGrayColor];
    [label sizeToFit];
    [self.view addSubview:label];
}

这这段代码中,其实我们想要显示高亮状态字符,重要的就是查找需要高亮显示的文字range了,看看官方文档里给的几个查找range的方法吧
86DE4C25-43D9-44C3-BE3C-0B774EA500C9.png

我的查找方法,就是用的文档里面的第一个方法,显然,在对于有重复字符串情况下,就不那么好使了。

重点来了!!!用这个方法,从后往前取需要匹配的string 记录range,然后更新searchRange.
  • -(NSRange)rangeOfString:(NSString *)searchString options:(NSStringCompareOptions)mask;

来看看NSStringCompareOptions的枚举里面的都代表什么吧


E41F0C38-6C32-4CC8-9B5A-115F13EACBED.png

这里的官方给的注释也很明确了,上述方法里面找到 range

NSRange range = [_pinChuan3 rangeOfString:content1 options:NSBackwardsSearch];

然后再次运行


1E941C3A-89D3-4400-BC6A-76A92ABE778F.png

没毛病! 解决!

      github地址,点击下载Demo

相关文章

网友评论

      本文标题:iOS 关于字符串有相同字符的情况下 查找range的问题

      本文链接:https://www.haomeiwen.com/subject/okxcdxtx.html