如何突出显示图像中的文字? [英] How to highlight text like in the image?

查看:159
本文介绍了如何突出显示图像中的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何突出显示图片中的手机文字?

How to highlight the text "cell phone" in the image?

已更新

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

I want to search text on a page and if found, the text should be highlighted in the way I have shown in the attached image.

推荐答案

输出

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

This Regular Expressions tutorial has detail on how to search the desired text using regular expression on UITextview and how to highlight it.

要使用的基本代码:

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

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

1) Following code searches for the required pattern from the data i.e 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和标签:

2) Here we highlight the matches obtained from the result of regular expression matching by making use of CALayer and 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:

Function used to obtain the frame of the matched text in the textView:

- (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

 }

这篇关于如何突出显示图像中的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆