【问题标题】:How to highlight text like in the image?如何突出显示图像中的文本?
【发布时间】:2013-05-25 13:42:55
【问题描述】:

如何高亮图片中的“手机”文字?

更新

我想搜索页面上的文本,如果找到,文本应该以我在附加图像中显示的方式突出显示。

【问题讨论】:

    标签: iphone ios search highlight


    【解决方案1】:

    输出

    Regular Expressions tutorial 详细介绍了如何在UITextview 上使用正则表达式搜索所需文本以及如何突出显示它。

    要使用的基本代码:

    - (void)viewDidLoad
    {
      [super viewDidLoad];
      [self searchData];
    }
    

    1) 以下代码从数据中搜索所需的模式,即 TextView:

     - (void) searchData
    {
    
    
        NSError *error = NULL;
        NSString *pattern = @"Hello";  // pattern to search thee data either regular expression or word.
        NSString *string = self.textView.text;
        NSRange range = NSMakeRange(0, string.length);
        NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive error:&error];
        NSArray *matches = [regex matchesInString:string options:NSMatchingProgress ran     ge:range];
       [self highlightMatches:matches];
    }
    

    2) 这里我们使用 CALayer 和 label 突出显示从正则表达式匹配的结果中得到的匹配:

     - (void)highlightMatches:(NSArray *)matches
     {
    
    
          __block NSMutableAttributedString *mutableAttributedString = self.textView.attributedText.mutableCopy;
         [matches enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
        {
    
         if ([obj isKindOfClass:[NSTextCheckingResult class]])
         {
             NSTextCheckingResult *match = (NSTextCheckingResult *)obj;
             CGRect rect = [self frameOfTextRange:match.range inTextView:self.textView];
    
             /** Shadow */
    
             CALayer *shadowLayer = [CALayer new];
             shadowLayer.frame = CGRectMake(rect.origin.x, rect.origin.y-4, rect.size.width, rect.size.height);
             shadowLayer.cornerRadius = 5;
    
             shadowLayer.backgroundColor = [UIColor yellowColor].CGColor;
             shadowLayer.shadowColor = [UIColor blackColor].CGColor;
             shadowLayer.shadowOpacity = 0.6;
             shadowLayer.shadowOffset = CGSizeMake(1,1);
             shadowLayer.shadowRadius = 3;
    
             /** Label */
             UILabel *lbl = [[UILabel alloc] initWithFrame:CGRectMake(rect.origin.x, rect.origin.y-4, rect.size.width, rect.size.height)];
             lbl.font = [UIFont fontWithName:@"Helvetica" size:12];
             lbl.textColor = [UIColor blackColor];
             [lbl setText:[[mutableAttributedString attributedSubstringFromRange:match.range] string]];
             lbl.backgroundColor = [UIColor clearColor];
             lbl.textAlignment = NSTextAlignmentCenter;
             lbl.layer.cornerRadius = 10;
    
             /** Add Label and layer*/
    
             [self.view.layer addSublayer:shadowLayer];
             [self.view addSubview:lbl];  
          }
      }];
    
      }
    

    获取textView中匹配文本的frame的函数:

    - (CGRect)frameOfTextRange:(NSRange)range inTextView:(UITextView *)textView
     {
        UITextPosition *beginning = textView.beginningOfDocument; //Error=: request for member 'beginningOfDocument' in something not a structure or union
    
        UITextPosition *start = [textView positionFromPosition:beginning offset:range.location];
        UITextPosition *end = [textView positionFromPosition:start offset:range.length];
        UITextRange *textRange = [textView textRangeFromPosition:start toPosition:end];
        CGRect rect = [textView firstRectForRange:textRange];  //Error: Invalid Intializer
    
        return [textView convertRect:rect fromView:textView.textInputView]; // Error: request for member 'textInputView' in something not a structure or union
    
     }
    

    【讨论】:

    • @Sandy 听起来很棒!乐于助人:)
    • 您能帮我从匹配字符串中获取 CGRect 吗?
    • 使用这种方法我相信不可能得到匹配字符串的CGRect。
    • 是的,我知道,但我正在努力解决我没有得到正确的 CGrect 在我的情况下起源不正确,长度和宽度都很好。我应该显示代码吗?
    • @VinayakKini,我将您的 frameOfTextRange:inTextView: 移植到 Swift 以在此处使用它:stackoverflow.com/a/34050391/106435 感谢您在这里的回答。
    【解决方案2】:

    使用NSMutableAttributedString 这可能对你有用。

    NSString *myString = @"Hello";
    NSMutableAttributedString *aString = [[NSMutableAttributedString alloc] initWithString:myString];
    NSRange theRange = NSMakeRange(0,[aString length]);
    

    //这里我们为文本设置前景色和背景色 突出显示它。

    [aString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"MyriadPro-Bold" size:17.5f] range:theRange];
    [aString addAttribute:NSForegroundColorAttributeName value:[UIColor purpleColor] range:theRange];
    [aString addAttribute:NSBackgroundColorAttributeName value:[UIColor yellowColor] range:theRange];
    

    //您可以根据您的文本部分更改范围 想要突出显示。

    【讨论】:

    • 您的代码无法正常工作,甚至崩溃。我已修复并将其传递给 UIlabel,但我看不到更改。请建议我是否做错了?
    • 我想你一定做过这样的事情。 UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 100, 100)]; [myLabel setText:aString];而不是下面的设置文本 [myLabel setAttributedText:aString];希望它有效..
    • 此代码是否提供圆角背景,如 OP 所示?
    【解决方案3】:

    为此,您必须使用 core Text 和突出显示您必须在 drawRect 方法中绘制文本,即将背景颜色更改为黄色并增加文本的字体大小

    tutorial on coreText

    【讨论】:

      【解决方案4】:

      There is a github repository "HighlightedWebView" 几乎可以达到您想要的效果。

      添加 Safari 风格的页内搜索结果突出显示的插入式 WebView 子类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-25
        • 1970-01-01
        • 1970-01-01
        • 2019-06-04
        相关资源
        最近更新 更多