UIWebView中搜索,并且高亮显示搜索内容。
先前有个项目要对webView中的内容进行搜索,并且要对搜索到的内容显示高亮状态,在前辈的指点下,其实发现也不是那么难的。
具体如下:(1)创建Empty文件,将文件后缀改写成:js。
(2)主要功能在如下的js代码中。将下面的代码复制到先前创建的js文件中。
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++; // update the counter } } else if (element.nodeType == 1) { // Element node 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); return true; } 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(); } } } } return false; } // 去除高亮状态 function MyApp_RemoveAllHighlights() { MyApp_SearchResultCount = 0; MyApp_RemoveAllHighlightsForElement(document.body); }
(3)创建UIWebView的类别:定义显示高亮状态和去除高亮2种方法。
@interface UIWebView (SearchWebView) - (NSInteger)highlightAllOccurencesOfString:(NSString*)str; - (void)removeAllHighlights;
接下来我们就要读取js文件里面的内容了。
@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好了,做到这里工作基本就完成了,后面要做的就是调用类别里的方法了,将搜索的关键字传入到方法里就可以了。