滚动到UIWebView上的文本

Van*_*nel 2 javascript cocoa-touch uiwebview ios

我正在开发一个带有最新SDK和XCode 4.2的iOS应用程序.

我想在UIWebview中搜索文本并滚动到找到的第一个文本.

我正在使用本教程查找文本:http://zaldzbugz.posterous.com/how-to-search-a-string-inside-uiwebview

如何滚动到找到的第一个文本?

Vig*_*esh 5

我们一步一步走吧.

突出

  1. 在您搜索的文本周围添加span元素.对于跨度,高亮颜色设置为跨度的背景.修改后的HTML字符串将在视图中呈现.

您使用的代码包含以下代码段.

    var span = document.createElement("span");
        var text = document.createTextNode(value.substr(idx,keyword.length));
            span.appendChild(text);

            span.setAttribute("class","uiWebviewHighlight");
            span.style.backgroundColor="black";
            span.style.color="white";
Run Code Online (Sandbox Code Playgroud)

我们需要一个id到跨度移动.所以添加id如下,

      var span = document.createElement("span");
        var text = document.createTextNode(value.substr(idx,keyword.length));
            span.appendChild(text);
            span.setAttribute("id","SEARCH WORD");
            span.setAttribute("class","uiWebviewHighlight");
            span.style.backgroundColor="black";
            span.style.color="white";
Run Code Online (Sandbox Code Playgroud)

移至第一次出现.

使用'Webview didload'中的以下javascript移动到第一次出现.

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('SEARCH WORD').scrollIntoView()];
Run Code Online (Sandbox Code Playgroud)

希望这是有帮助的.