Rechercher pour surligner du text en PDF en utilisant UIWebView

Je suis nouveau dans le développement d'IOS J'ai UIWebView pour des articles d'affichage de page de Pdf d'URL. Je dois searchr la string ou le text et surligner. Je ne peux pas searchr et surligner le text dans le pdf en utilisant UIWebView.PDF charge bien mais le text de search ne met pas en évidence. son travail uniquement pour le format HTML dont j'ai besoin en PDF

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 URLWithSsortingng:@"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 highlightAllOccurencesOfSsortingng:@"guide"]; } -(void)webViewDidFinishLoad:(UIWebView *)webView { [webView1 highlightAllOccurencesOfSsortingng:@"guide"]; } @end 

SearchWebView.h

 #import <Foundation/Foundation.h> @interface SearchWebView : UIWebView - (NSInteger)highlightAllOccurencesOfSsortingng:(NSSsortingng*)str; - (void)removeAllHighlights; @end 

SearchWebView.m

 #import "SearchWebView.h" @implementation SearchWebView - (NSInteger)highlightAllOccurencesOfSsortingng:(NSSsortingng*)str { NSSsortingng *path = [[NSBundle mainBundle] pathForResource:@"SearchWebView" ofType:@"js"]; NSSsortingng *jsCode = [NSSsortingng ssortingngWithContentsOfFile:path encoding:NSUTF8SsortingngEncoding error:nil]; [self ssortingngByEvaluatingJavaScriptFromSsortingng:jsCode]; NSSsortingng *startSearch = [NSSsortingng ssortingngWithFormat:@"MyApp_HighlightAllOccurencesOfSsortingng('%@')",str]; [self ssortingngByEvaluatingJavaScriptFromSsortingng:startSearch]; NSSsortingng *result = [self ssortingngByEvaluatingJavaScriptFromSsortingng:@"MyApp_SearchResultCount"]; return [result integerValue]; } - (void)removeAllHighlights { [self ssortingngByEvaluatingJavaScriptFromSsortingng:@"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_HighlightAllOccurencesOfSsortingngForElement(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.setAtsortingbute("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_HighlightAllOccurencesOfSsortingngForElement(element.childNodes[i],keyword); } } } } } // the main entry point to start the search function MyApp_HighlightAllOccurencesOfSsortingng(keyword) { MyApp_RemoveAllHighlights(); MyApp_HighlightAllOccurencesOfSsortingngForElement(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.getAtsortingbute("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); } 

Dans mon cas, j'ai recherché du text à partir d'un chargement de page dans la vue Web à l'aide du text entré dans la barre de search & mon SearchWebView.js

 var uiWebview_SearchResultCount = 0; /*! @method uiWebview_HighlightAllOccurencesOfSsortingngForElement @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 - ssortingng to search */ function uiWebview_HighlightAllOccurencesOfSsortingngForElement(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.setAtsortingbute("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_HighlightAllOccurencesOfSsortingngForElement(element.childNodes[i],keyword); } } } } } // the main entry point to start the search function uiWebview_HighlightAllOccurencesOfSsortingng(keyword) { uiWebview_RemoveAllHighlights(); uiWebview_HighlightAllOccurencesOfSsortingngForElement(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.getAtsortingbute("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); } 

Et dans mon .m j'ai chercher n'importe quel text entrer dans la barre de search comme:

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

Et ma méthode highlightAllOccurencesOfSsortingng fonctionne comme:

 - (NSInteger)highlightAllOccurencesOfSsortingng:(NSSsortingng*)str { NSSsortingng *filePath = [[NSBundle mainBundle] pathForResource:@"UIWebViewSearch" ofType:@"js" inDirectory:@""]; NSData *fileData = [NSData dataWithContentsOfFile:filePath]; NSSsortingng *jsSsortingng = [[NSMutableSsortingng alloc] initWithData:fileData encoding:NSUTF8SsortingngEncoding]; [webView ssortingngByEvaluatingJavaScriptFromSsortingng:jsSsortingng]; NSSsortingng *startSearch = [NSSsortingng ssortingngWithFormat:@"uiWebview_HighlightAllOccurencesOfSsortingng('%@')",str]; [webView ssortingngByEvaluatingJavaScriptFromSsortingng:startSearch]; NSSsortingng *result = [webView ssortingngByEvaluatingJavaScriptFromSsortingng:@"uiWebview_SearchResultCount"]; return [result integerValue]; } 

Et ma méthode removeAllHighlights est:

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

Comparez maintenant tout cela avec votre situation. J'espère que cela vous aide. Si des doutes alors libre de me requestr.