我正在開發一個包含最新SDK和XCode 4.2的iOS應用程序。滾動到UIWebView上的文本
我想搜索UIWebview中的文本並滾動到找到的第一個文本。
我使用本教程中查找文本:http://zaldzbugz.posterous.com/how-to-search-a-string-inside-uiwebview
如何我可以滾動到第一個文本中?
我正在開發一個包含最新SDK和XCode 4.2的iOS應用程序。滾動到UIWebView上的文本
我想搜索UIWebview中的文本並滾動到找到的第一個文本。
我使用本教程中查找文本:http://zaldzbugz.posterous.com/how-to-search-a-string-inside-uiwebview
如何我可以滾動到第一個文本中?
看看您用來突出顯示的教程的最後一條評論。 http://zaldzbugz.posterous.com/how-to-search-a-string-inside-uiwebview
所有操作後添加的代碼塊到UIWebViewSearch.js與跨度
//old code
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;
//new portion of code
if (uiWebview_SearchResultCount == 1)
{
var desiredHeight = span.offsetTop - 140;
window.scrollTo(0,desiredHeight);
}
我檢查這個解決方案在iPhone模擬器4.3和5.0。
讓我們一步一步來。
亮點
您使用的代碼有以下代碼片段。
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的跨度move.So添加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()];
希望這會有幫助。
您可以滾動與此修改中最頂端的文字:
// >=1
if (uiWebview_SearchResultCount >= 1)
{
var desiredHeight = span.offsetTop - 140;
window.scrollTo(0,desiredHeight);
}
我如何可以滾動到最上面的文本中找到的? – VansFannel 2012-02-17 14:42:54
當uiWebview_SearchResultCount等於1時,這意味着當前跨度突出顯示突出顯示的文本的第一個出現。 – vrmaks 2012-02-21 09:25:36