温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ios 在 UIWebView中搜索并高亮度显示关键字

发布时间:2020-07-16 08:49:49 来源:网络 阅读:550 作者:ljl5125 栏目:移动开发

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)



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI