2013-11-08 4 views
3

Я новичок в разработке ios У меня есть UIWebView для отображения Pdf страниц с URL. Мне нужно искать строку или текст и выделять. Я не могу искать и выделять текст в PDF, используя UIWebView.Pdf загружается отлично, но текст поиска не выделяется. его работать только для HTML, мне нужно в PDFПоиск, чтобы выделить текст в PDF с использованием UIWebView

Mycode

ViewController.h

#import <UIKit/UIKit.h> 
#import "SearchWebView.h" 
@interface ViewController : UIViewController<UIWebViewDelegate> 
{ 
IBOutlet SearchWebView *webView1; 
} 
@end 

ViewController.m

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 
NSURL *targetURL = [NSURL URLWithString:@"http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; 
webView1.delegate=self; 
[webView1 loadRequest:request]; 
[webView1 highlightAllOccurencesOfString:@"guide"]; 
} 

-(void)webViewDidFinishLoad:(UIWebView *)webView 
{ 
[webView1 highlightAllOccurencesOfString:@"guide"]; 
} 

@end 

SearchWebView.h

#import <Foundation/Foundation.h> 

@interface SearchWebView : UIWebView 

- (NSInteger)highlightAllOccurencesOfString:(NSString*)str; 
- (void)removeAllHighlights; 

@end 

SearchWebView.m

#import "SearchWebView.h" 

@implementation SearchWebView 

- (NSInteger)highlightAllOccurencesOfString:(NSString*)str 
{ 
NSString *path = [[NSBundle mainBundle] pathForResource:@"SearchWebView" ofType:@"js"]; 
NSString *jsCode = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; 
[self stringByEvaluatingJavaScriptFromString:jsCode]; 

NSString *startSearch = [NSString stringWithFormat:@"MyApp_HighlightAllOccurencesOfString('%@')",str]; 
[self stringByEvaluatingJavaScriptFromString:startSearch]; 

NSString *result = [self stringByEvaluatingJavaScriptFromString:@"MyApp_SearchResultCount"]; 
return [result integerValue]; 
} 

- (void)removeAllHighlights 
{ 
[self stringByEvaluatingJavaScriptFromString:@"MyApp_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); 
} 
+0

Вы использовали панель поиска для хранения текста в формате PDF? – Vishal

+0

не использует строку поиска progromatically. Я собираюсь искать текст [webView1 highlightAllOccurencesOfString: @ "guide"]; – Ravindhiran

ответ

2

В моем случае, я искал текст с загрузки страницы в веб-просмотра через текст, введите в строке поиска & my SearchWebView.js

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); 
} 

И в моей .m У меня есть поиск любой текст, введите в строке поиска, как:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar 
{ 
    [self removeAllHighlights]; 
    int resultCount = [self highlightAllOccurencesOfString:searchBar.text]; 
    if (resultCount <= 0) 
    { 
     alert = [[UIAlertView alloc] initWithTitle:@"No Match Found!" 
                 message:[NSString stringWithFormat:@"No results found for this: %@", searchBar.text] 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 

    } 
    [searchBar resignFirstResponder]; 
} 

И мой метод highlightAllOccurencesOfString работает как:

- (NSInteger)highlightAllOccurencesOfString:(NSString*)str 
{ 

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js" inDirectory:@""]; 
    NSData *fileData = [NSData dataWithContentsOfFile:filePath]; 
    NSString *jsString = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding]; 
    [webView stringByEvaluatingJavaScriptFromString:jsString]; 
    NSString *startSearch = [NSString stringWithFormat:@"uiWebview_HighlightAllOccurencesOfString('%@')",str]; 
    [webView stringByEvaluatingJavaScriptFromString:startSearch]; 
    NSString *result  = [webView stringByEvaluatingJavaScriptFromString:@"uiWebview_SearchResultCount"]; 
    return [result integerValue]; 
} 

И мой метод removeAllHighlights это:

- (void)removeAllHighlights 
{ 
    [webView stringByEvaluatingJavaScriptFromString:@"uiWebview_RemoveAllHighlights()"]; 
} 

Теперь сравните все это с вашей ситуацией. Надеюсь, это поможет вам. Если какие-то сомнения тогда свободны спросить меня.

+0

pdf загружается отлично, но результат поиска всегда возвращается 0 Я не знаю, почему я получаю 0 – Ravindhiran

+0

- (void) webViewDidFinishLoad: (UIWebView *) webView { int resultCount = [webView1 highlightAllOccurencesOfString: @ "guide"]; NSLog (@ "% d", resultCount); } – Ravindhiran

+0

Текст, который вы искали в веб-представлении, действительно на нем? – Vishal

Смежные вопросы