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

如何突出图片中的文字“手机”?

在这里输入图像说明

更新

我想search页面上的文本,如果发现,文本应该以我在附图中显示的方式突出显示。

产量

输出图像

此正则expression式教程详细介绍了如何在UITextview上使用正则expression式search所需文本以及如何突出显示。

要使用的基本代码:

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

1)以下代码从数据中search所需的模式,即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和标签来突出显示从正则expression式匹配结果中获得的匹配:

  - (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中匹配文本框架的函数:

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

使用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]; 

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

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

coreText教程

有一个github仓库“HighlightedWebView” ,实际上达到了你所希望的效果。

在这里输入图像说明

embedded式WebView子类,用于添加Safari风格的页内search结果高亮显示。

我find了一个链接,我认为它有帮助。 链接在这里

它指出

如果您想突出显示文本(或支付许可中间件解决scheme),则必须编写自己的自定义PDF文本parsing器.PDF格式是旧的,并且已经由Adobe添加到其中,其中一些已经是现在多余的。 PDF文档超过700页,并没有特别清晰的写法,特别是当你第一次使用它的时候。 此外,还有许多不同的PDF编写器,每个编写器都有自己的格式解释,并使用不同的命令和命令组合来定位文本。

要突出显示单词,您需要parsing每个页面(使用CGPDF调用)并提取文本和位置数据。 然后使用叠加在您的渲染的PDF页面顶部绘制突出显示的块。

我认为这有帮助。

Interesting Posts