UIWebView内容搜索并且显示高亮背景颜色以及改变字体的背景颜色(不是字体颜色)
第一次写博,废话不多说,直接话题,对于WebView内容搜索以及显示高亮,搜索就不介绍,方法很多,重点就发在高亮显示。具体步骤如下:
*1.创建empty文件,为了自我方便,将文件命名:SearchWebView后置改为.js
*2将搜索内容显示高亮代码:
function MyApp_HighlightAllOccurencesOfStringForElement(element,keyword) {
if (element) {
if (element.nodeType ==3) {
while (true) {
var value = element.nodeValue;
var idx = value.toLowerCase().indexOf(keyword);
if (idx <0)break;
var span = document.createElement("span");
var text = document.createTextNode(value.substr(idx,keyword.length));
span.appendChild(text);
span.setAttribute("class","MyAppHighlight");
span.style.backgroundColor="yellow";
span.style.color="black";
text = document.createTextNode(value.substr(idx+keyword.length));
element.deleteData(idx, value.length - idx);
var next = element.nextSibling;
element.parentNode.insertBefore(span, next);
element.parentNode.insertBefore(text, next);
element = text;
MyApp_SearchResultCount++;
}
} else if (element.nodeType == 1) {
if (element.style.display !="none" && element.nodeName.toLowerCase() !='select') {
for (var i=element.childNodes.length-1; i>=0; i--) {
MyApp_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
}
}
}
}
}
function MyApp_HighlightAllOccurencesOfString(keyword) {
MyApp_RemoveAllHighlights();
MyApp_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());
}
function MyApp_RemoveAllHighlightsForElement(element) {
if (element) {
if (element.nodeType ==1) {
if (element.getAttribute("class") =="MyAppHighlight") {
var text = element.removeChild(element.firstChild);
element.parentNode.insertBefore(text,element);
element.parentNode.removeChild(element);
returntrue;
} else {
var normalize =false;
for (var i=element.childNodes.length-1; i>=0; i--) {
if (MyApp_RemoveAllHighlightsForElement(element.childNodes[i])) {
normalize = true;
}
}
if (normalize) {
element.normalize();
}
}
}
}
returnfalse;
}
function MyApp_RemoveAllHighlights() {
MyApp_SearchResultCount = 0;
MyApp_RemoveAllHighlightsForElement(document.body);
}
*3.创建UIWebView类别,定义方法。
SearchWebView.h:
@interface UIWebView (SearchWebView) - (NSInteger)highlightAllOccurencesOfString:(NSString*)str; - (void)removeAllHighlights; @end实现方法:(其实主要还是*2中的 JavaScrip代码)这边仅仅就是一个调用而已了
SearchWebView.m:
@implementation UIWebView (SearchWebView) //显示高亮 - (NSInteger)highlightAllOccurencesOfString:(NSString*)str { NSString *path = [[NSBundle mainBundle] pathForResource:@"SearchWebView" ofType:@"js"]; NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; [self stringByEvaluatingJavaScriptFromString:jsCode]; NSString *startSearch = [NSString stringWithFormat:@"MyApp_HighlightAllOccurencesOfString('%@')",str]; [self stringByEvaluatingJavaScriptFromString:startSearch]; NSString *result = [self stringByEvaluatingJavaScriptFromString:@"MyApp_SearchResultCount"]; return [result integerValue]; } //还原 - (void)removeAllHighlights { [self stringByEvaluatingJavaScriptFromString:@"MyApp_RemoveAllHighlights()"]; } @end*4.完成到这里基本就完成,剩下的就是在合适的地方调用方法,以及搜索内容的传入。
二:改变字体背景颜色
*1.创建label子类:
#import <UIKit/UIKit.h>
#import <CoreText/CoreText.h>
#import <QuartzCore/QuartzCore.h>
@interface AttributedLabel : UILabel{
NSMutableAttributedString *_attString;
}
// 设置某段字的颜色
- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length;
@end
*2.m文件实现- (void)setColor:(UIColor *)color fromIndex:(NSInteger)location length:(NSInteger)length{
NSMutableAttributedString *str = [[NSMutableAttributedStringalloc]initWithAttributedString:self.attributedText];
//奇怪的现象,在ios8下需要加上下面这段代码才可以实现。
// [straddAttribute:NSBackgroundColorAttributeNamevalue:[UIColorclearColor]range:NSMakeRange(0,str.length)];
[str addAttribute:NSBackgroundColorAttributeNamevalue:colorrange:NSMakeRange(location,length)];
self.attributedText = str;
}