使用swift在webview页面中查找文本? [英] Find text in webview page using swift?

查看:141
本文介绍了使用swift在webview页面中查找文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在uiwebview页面中找到文本并突出显示它并使用swift转到页面中的位置?

How can I find text in uiwebview page and highlight it and go to its position in the page using swift ?

例如chrome查找文本:

For example chrome find text :

搜索示例

但我改为使用提醒视图:

But I'm using alert view instead :

我的应用程序中的搜索示例!!!

推荐答案

这是快速代码:

1)将UIWebViewSearch.js文件添加到项目中

1) add UIWebViewSearch.js file to your project

func highlightAllOccurencesOfString(str:String) -> Int {

    let path = Bundle.main.path(forResource: "UIWebViewSearch", ofType: "js")

    var jsCode = ""

    do{

        jsCode = try String(contentsOfFile: path!)

    }catch
    {
    // do something

    }

    self.newsWebView.stringByEvaluatingJavaScript(from: jsCode)

    let startSearch = "uiWebview_HighlightAllOccurencesOfString('\(str)')"

    self.newsWebView .stringByEvaluatingJavaScript(from: startSearch)

    let  result = self.newsWebView.stringByEvaluatingJavaScript(from: "uiWebview_SearchResultCount")

    return  Int(result!)!


}

func scrollTo(index:Int)  {

    self.newsWebView.stringByEvaluatingJavaScript(from: "uiWebview_ScrollTo('\(index)')")

}

func removeAllHighlights() {

self.newsWebView.stringByEvaluatingJavaScript(from: "uiWebview_RemoveAllHighlights()")

} 

这篇关于使用swift在webview页面中查找文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆