iphone - 如何突出显示图像中的文字?

标签 iphone ios search highlight

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

enter image description here

已更新

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

最佳答案

输出

OutPut Image

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

 }

关于iphone - 如何突出显示图像中的文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16828425/

相关文章:

ios - UITextField 子类,下方有栏 : bar not going all the way

ios - 如何在Firebase中使用writeToFile来处理超出最大大小的情况?

iphone - 有没有办法在不重新启动应用程序的情况下使 NSBundle 本地化缓存失效? [iOS]

iphone - Cocoa-Touch,核心数据 : Linker error, 未找到 NSManagedObject 符号

ios - 如何/在哪里设置委托(delegate)

search - jqGrid 过滤记录

python - 如何深入搜索 Python 列表?

python - 正则表达式?搜索引擎?

ios - 从 Swift 3 中的字节数组中读取值

ios - 在iOs中,是否可以通过编程方式停止从AppStore自动更新我的应用程序?