如何在图像中突出显示“手机”文字?
更新
我想在网页上搜索文字,如果找到,则应按照我在附加图片中显示的方式突出显示文字。
答案 0 :(得分:17)
<强>输出强>
此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和标签突出显示从正则表达式匹配结果中获得的匹配:
- (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
}
答案 1 :(得分:5)
使用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];
//您可以根据您的文本部分更改范围 想突出显示。
答案 2 :(得分:2)
为此你必须使用core Text
&amp;要突出显示,您必须使用drawRect
方法绘制文本,即将背景颜色更改为黄色&amp;增加文字的字体大小
答案 3 :(得分:2)
There is a github repository "HighlightedWebView"实现与您所希望的几乎相同的效果。
Drop-in WebView子类,增加了Safari风格的页内搜索结果突出显示。