滚动到UIWebView上的文字 [英] Scroll to text on UIWebView

查看:145
本文介绍了滚动到UIWebView上的文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用最新的SDK和XCode 4.2开发一个iOS应用程序。



我想在UIWebview中搜索文本并滚动到找到的第一个文本。 / p>

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



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

解决方案

让我们一步一步。



突出显示


  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;

我们需要一个id到span来移动。添加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;

移至第一次。



在Webview didload中使用以下javascript来移动到第一次出现。

  [webView stringByEvaluatingJavaScriptFromString :[NSString stringWithFormat:@document.getElementById('SEARCH WORD')。scrollIntoView()]; 

希望这是有帮助的。


I'm developing an iOS application with latest SDK and XCode 4.2.

I want to search a text in a UIWebview and scroll to the first text found.

I'm using this tutorial to find text: http://zaldzbugz.posterous.com/how-to-search-a-string-inside-uiwebview

How can I scroll to first text found?

解决方案

Let's go step by step.

Highlight

  1. A span element is added around the text you search for. For the span, highlight color is set as span's background. The modified HTML string is rendered in the view.

The code you used has the following snippet.

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

We need to a id to the span to move.So add id as follows,

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

Moving to the First occurrence.

Use the following javascript in 'Webview didload' to move to the first occurence.

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.getElementById('SEARCH WORD').scrollIntoView()];

Hope this is helpful.

这篇关于滚动到UIWebView上的文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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