1.创建两个文件(SearchWebView.js(文章末尾有写),UIWebView+SearchWebView(类目))

引自:http://www.cnblogs.com/zhuolaiqiang/archive/2011/06/24/2088906.html



//js文件内容

SearchWebView.js:

var MyApp_SearchResultCount = 0;



function MyApp_HighlightAllOccurencesOfStringForElement(element,keyword) {

if (element) {

if (element.nodeType == 3) { // Text node

while (true) {

var value = element.nodeValue; // Search for keyword in text node

var idx = value.toLowerCase().indexOf(keyword);

if (idx < 0) break; // not found, abort

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="Red";

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);

}

}

}

}

}


// the main entry point to start the search

function MyApp_HighlightAllOccurencesOfString(keyword) {

MyApp_RemoveAllHighlights();

MyApp_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());

}


// helper function, recursively removes the highlights in elements and their childs

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;

}


// the main entry point to remove the highlights

function MyApp_RemoveAllHighlights() {

MyApp_SearchResultCount = 0;

MyApp_RemoveAllHighlightsForElement(document.body);

}



//UIWebView+SearchWebView.h

#import <UIKit/UIKit.h>


@interface UIWebView (SearchWebView)

- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;

- (void)removeAllHighlights;


@end


//UIWebView+SearchWebView.m

#import "UIWebView+SearchWebView.h"


@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



//在调用的UIWebView中调用搜索关键字,记住,一定要在加载网页后显示(写在-(void)webViewDidFinishLoad:代理方法里)

[webView1 highlightAllOccurencesOfString:@“搜索的关键字”];


Xcode 新建js文件
解决方法:新建一个文件(Other -> Empty ->命名为.js的文件)
Xcode编译WebApps找不到js的错误解决办法

解决方法:在targets -> Build Phases -> Copy Bundle Resources -> + (将js文件加进Copy Bundle Resources)