Ios 使用UIWebView搜索以突出显示PDF中的文本
我是ios开发的新手,我有UIWebView用于显示来自URL的Pdf页面文章。我需要搜索字符串或文本并突出显示。我无法使用UIWebView搜索并突出显示PDF中的文本。PDF加载正常,但搜索文本未突出显示。它仅适用于我需要的PDF中的html 麦可德 ViewController.hIos 使用UIWebView搜索以突出显示PDF中的文本,ios,iphone,objective-c,pdf,uiwebview,Ios,Iphone,Objective C,Pdf,Uiwebview,我是ios开发的新手,我有UIWebView用于显示来自URL的Pdf页面文章。我需要搜索字符串或文本并突出显示。我无法使用UIWebView搜索并突出显示PDF中的文本。PDF加载正常,但搜索文本未突出显示。它仅适用于我需要的PDF中的html 麦可德 ViewController.h #import <UIKit/UIKit.h> #import "SearchWebView.h" @interface ViewController : UIViewController<U
#import <UIKit/UIKit.h>
#import "SearchWebView.h"
@interface ViewController : UIViewController<UIWebViewDelegate>
{
IBOutlet SearchWebView *webView1;
}
@end
#import <Foundation/Foundation.h>
@interface SearchWebView : UIWebView
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;
- (void)removeAllHighlights;
@end
SearchWebView.h
#import <UIKit/UIKit.h>
#import "SearchWebView.h"
@interface ViewController : UIViewController<UIWebViewDelegate>
{
IBOutlet SearchWebView *webView1;
}
@end
#import <Foundation/Foundation.h>
@interface SearchWebView : UIWebView
- (NSInteger)highlightAllOccurencesOfString:(NSString*)str;
- (void)removeAllHighlights;
@end
SearchWebView.js
// We're using a global variable to store the number of occurrences
var MyApp_SearchResultCount = 0;
// helper function, recursively searches in elements and their child nodes
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="black";
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);
}
var uiWebview_SearchResultCount = 0;
/*!
@method uiWebview_HighlightAllOccurencesOfStringForElement
@abstract // helper function, recursively searches in elements and their child nodes
@discussion // helper function, recursively searches in elements and their child nodes
element - HTML elements
keyword - string to search
*/
function uiWebview_HighlightAllOccurencesOfStringForElement(element,keyword) {
if (element) {
if (element.nodeType == 3) { // Text node
while (true) {
//if (counter < 1) {
var value = element.nodeValue; // Search for keyword in text node
var idx = value.toLowerCase().indexOf(keyword);
if (idx < 0) break; // not found, abort
//(value.split);
//we create a SPAN element for every parts of matched keywords
var span = document.createElement("span");
var text = document.createTextNode(value.substr(idx,keyword.length));
span.appendChild(text);
span.setAttribute("class","uiWebviewHighlight");
span.style.backgroundColor="purple";
span.style.color="white";
uiWebview_SearchResultCount++; // update the counter
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;
}
} 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--) {
uiWebview_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
}
}
}
}
}
// the main entry point to start the search
function uiWebview_HighlightAllOccurencesOfString(keyword) {
uiWebview_RemoveAllHighlights();
uiWebview_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());
}
// helper function, recursively removes the highlights in elements and their childs
function uiWebview_RemoveAllHighlightsForElement(element) {
if (element) {
if (element.nodeType == 1) {
if (element.getAttribute("class") == "uiWebviewHighlight") {
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 (uiWebview_RemoveAllHighlightsForElement(element.childNodes[i])) {
normalize = true;
}
}
if (normalize) {
element.normalize();
}
}
}
}
return false;
}
// the main entry point to remove the highlights
function uiWebview_RemoveAllHighlights() {
uiWebview_SearchResultCount = 0;
uiWebview_RemoveAllHighlightsForElement(document.body);
}
//我们使用一个全局变量来存储出现的次数
var MyApp_SearchResultCount=0;
//helper函数,递归搜索元素及其子节点
函数MyApp\u highlightAlloccurrencesofstringforelement(元素,关键字){
if(元素){
如果(element.nodeType==3){//Text节点
while(true){
var value=element.nodeValue;//在文本节点中搜索关键字
var idx=value.toLowerCase().indexOf(关键字);
如果(idx<0)中断;//未找到,则中止
var span=document.createElement(“span”);
var text=document.createTextNode(value.substr(idx,keyword.length));
span.appendChild(文本);
setAttribute(“类”、“MyAppHighlight”);
span.style.backgroundColor=“黄色”;
span.style.color=“黑色”;
text=document.createTextNode(value.substr(idx+关键字.length));
element.deleteData(idx,value.length-idx);
var next=element.nextSibling;
element.parentNode.insertBefore(span,next);
element.parentNode.insertBefore(文本,下一步);
元素=文本;
MyApp_SearchResultCount++;//更新计数器
}
}如果(element.nodeType==1){//element节点
if(element.style.display!=“无”&&element.nodeName.toLowerCase()!=“选择”){
对于(var i=element.childNodes.length-1;i>=0;i--){
MyApp_HighlightAlloccurrenceSofsTringForElement(element.childNodes[i],关键字);
}
}
}
}
}
//开始搜索的主要入口点
函数MyApp\u HighlightAlloccurrencesofString(关键字){
MyApp_RemoveAllHighlights();
MyApp_HighlightAlloccurrenceSofsTringForElement(document.body,keyword.toLowerCase());
}
//helper函数,递归删除元素及其子元素中的高光
函数MyApp_RemoveAllHighlightsRelation(元素){
if(元素){
if(element.nodeType==1){
if(element.getAttribute(“类”)=“MyAppHighlight”){
var text=element.removeChild(element.firstChild);
element.parentNode.insertBefore(文本,元素);
element.parentNode.removeChild(元素);
返回true;
}否则{
var正常化=错误;
对于(var i=element.childNodes.length-1;i>=0;i--){
if(MyApp_removeallhighlightsfrelation(element.childNodes[i])){
正常化=正确;
}
}
如果(正常化){
元素。normalize();
}
}
}
}
返回false;
}
//删除高光的主要入口点
函数MyApp_RemoveAllHighlights(){
MyApp_SearchResultCount=0;
MyApp_移除所有高亮显示相关内容(document.body);
}
在我的例子中,我通过搜索栏中的文本输入搜索了web视图中加载的页面中的文本&mySearchWebView.js
// We're using a global variable to store the number of occurrences
var MyApp_SearchResultCount = 0;
// helper function, recursively searches in elements and their child nodes
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="black";
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);
}
var uiWebview_SearchResultCount = 0;
/*!
@method uiWebview_HighlightAllOccurencesOfStringForElement
@abstract // helper function, recursively searches in elements and their child nodes
@discussion // helper function, recursively searches in elements and their child nodes
element - HTML elements
keyword - string to search
*/
function uiWebview_HighlightAllOccurencesOfStringForElement(element,keyword) {
if (element) {
if (element.nodeType == 3) { // Text node
while (true) {
//if (counter < 1) {
var value = element.nodeValue; // Search for keyword in text node
var idx = value.toLowerCase().indexOf(keyword);
if (idx < 0) break; // not found, abort
//(value.split);
//we create a SPAN element for every parts of matched keywords
var span = document.createElement("span");
var text = document.createTextNode(value.substr(idx,keyword.length));
span.appendChild(text);
span.setAttribute("class","uiWebviewHighlight");
span.style.backgroundColor="purple";
span.style.color="white";
uiWebview_SearchResultCount++; // update the counter
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;
}
} 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--) {
uiWebview_HighlightAllOccurencesOfStringForElement(element.childNodes[i],keyword);
}
}
}
}
}
// the main entry point to start the search
function uiWebview_HighlightAllOccurencesOfString(keyword) {
uiWebview_RemoveAllHighlights();
uiWebview_HighlightAllOccurencesOfStringForElement(document.body, keyword.toLowerCase());
}
// helper function, recursively removes the highlights in elements and their childs
function uiWebview_RemoveAllHighlightsForElement(element) {
if (element) {
if (element.nodeType == 1) {
if (element.getAttribute("class") == "uiWebviewHighlight") {
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 (uiWebview_RemoveAllHighlightsForElement(element.childNodes[i])) {
normalize = true;
}
}
if (normalize) {
element.normalize();
}
}
}
}
return false;
}
// the main entry point to remove the highlights
function uiWebview_RemoveAllHighlights() {
uiWebview_SearchResultCount = 0;
uiWebview_RemoveAllHighlightsForElement(document.body);
}
我的removeAllHighlights方法是:
- (void)removeAllHighlights
{
[webView stringByEvaluatingJavaScriptFromString:@"uiWebview_RemoveAllHighlights()"];
}
现在将所有这些与你的情况进行比较。希望对你有帮助。如果有任何疑问,请随时问我。您是否使用搜索栏搜索PDF格式的文本?如果不使用搜索栏,我将搜索文本[webView1 HighlightAlloccurrencesofString:@“guide”];如何搜索下面这样的多个单词?“路径”谢谢:)pdf正在加载,但搜索结果计数始终返回0我不知道为什么我得到0-(无效)webViewDidFinishLoad:(UIWebView*)webView{int resultCount=[webView1 highlightAlloccurrencesofString:@“guide”];NSLog(@“%d”,resultCount);}您在web视图中搜索的文本确实在其上?使用此int-resultCount=[self-highlightalloccurrencesofstring:@“guide”];也在这一行的上方:[webView1 HighlightAlloccurrencesofString:@“guide”];