溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

ios 在 UIWebView中搜索并高亮度顯示關鍵字

發布時間:2020-07-16 08:49:49 來源:網絡 閱讀:579 作者: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

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女