• <tfoot id='kOtZL'></tfoot>

      <small id='kOtZL'></small><noframes id='kOtZL'>

      <i id='kOtZL'><tr id='kOtZL'><dt id='kOtZL'><q id='kOtZL'><span id='kOtZL'><b id='kOtZL'><form id='kOtZL'><ins id='kOtZL'></ins><ul id='kOtZL'></ul><sub id='kOtZL'></sub></form><legend id='kOtZL'></legend><bdo id='kOtZL'><pre id='kOtZL'><center id='kOtZL'></center></pre></bdo></b><th id='kOtZL'></th></span></q></dt></tr></i><div id='kOtZL'><tfoot id='kOtZL'></tfoot><dl id='kOtZL'><fieldset id='kOtZL'></fieldset></dl></div>

          <bdo id='kOtZL'></bdo><ul id='kOtZL'></ul>
      1. <legend id='kOtZL'><style id='kOtZL'><dir id='kOtZL'><q id='kOtZL'></q></dir></style></legend>

        使用 UIWebView 搜索以突出显示 PDF 中的文本

        Search to highlight text in PDF using UIWebView(使用 UIWebView 搜索以突出显示 PDF 中的文本)
          <tbody id='OEc4o'></tbody>

          <legend id='OEc4o'><style id='OEc4o'><dir id='OEc4o'><q id='OEc4o'></q></dir></style></legend>

          <small id='OEc4o'></small><noframes id='OEc4o'>

        1. <i id='OEc4o'><tr id='OEc4o'><dt id='OEc4o'><q id='OEc4o'><span id='OEc4o'><b id='OEc4o'><form id='OEc4o'><ins id='OEc4o'></ins><ul id='OEc4o'></ul><sub id='OEc4o'></sub></form><legend id='OEc4o'></legend><bdo id='OEc4o'><pre id='OEc4o'><center id='OEc4o'></center></pre></bdo></b><th id='OEc4o'></th></span></q></dt></tr></i><div id='OEc4o'><tfoot id='OEc4o'></tfoot><dl id='OEc4o'><fieldset id='OEc4o'></fieldset></dl></div>
            <bdo id='OEc4o'></bdo><ul id='OEc4o'></ul>
              1. <tfoot id='OEc4o'></tfoot>
                  本文介绍了使用 UIWebView 搜索以突出显示 PDF 中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我是 ios 开发的新手,我有 UIWebView 用于显示来自 URL 的 Pdf 页面文章.我需要搜索字符串或文本并突出显示.我无法使用 UIWebView 搜索和突出显示 PDF 中的文本.PDF 加载正常,但搜索文本没有突出显示.它仅适用于我在 PDF 中需要的 html

                  I am new in ios developement I have UIWebView for displays Pdf page articles from URL. I need to search string or text and highlight.I am not able to search and highlight the text in PDF using UIWebView.Pdf is loading fine but search text is not highlighting.its working only for html I need in PDF

                  我的代码

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

                  推荐答案

                  在我的例子中,我已经通过在搜索栏中输入文本从 Web 视图中的页面加载中搜索文本 &我的 SearchWebView.js

                  In my case, I have searched text from a page load in web view through text enter in Search bar & 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 中,我搜索了在搜索栏中输入的任何文本,例如:

                  And in my .m I have search any text enter in search bar like:

                  - (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 方法的工作方式如下:

                  And my highlightAllOccurencesOfString method is working like:

                  - (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 方法是:

                  And my removeAllHighlights method is:

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

                  现在将所有这些与您的情况进行比较.希望它可以帮助你.如果有任何疑问,请随时问我.

                  Now compare all this with your situation. Hope it helps you. If any doubts then free to ask me.

                  这篇关于使用 UIWebView 搜索以突出显示 PDF 中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  iOS UIWebView app opens link in Safari(iOS UIWebView 应用在 Safari 中打开链接)
                  Close UIWebView using javascript:window.close();(使用 javascript:window.close() 关闭 UIWebView;)
                  NSCachedURLResponse returns object, but UIWebView does not interprets content(NSCachedURLResponse 返回对象,但 UIWebView 不解释内容)
                  Trouble hiding iAd banner and displaying UIWebView in its place(无法隐藏 iAd 横幅并在其位置显示 UIWebView)
                  Knowing when AJAX has loaded in UIWebView(知道何时在 UIWebView 中加载了 AJAX)
                  Load image/logo in existing HTML input field - UIWebView(在现有的 HTML 输入字段中加载图像/徽标 - UIWebView)

                  1. <tfoot id='Lb0Mk'></tfoot>
                      <bdo id='Lb0Mk'></bdo><ul id='Lb0Mk'></ul>
                        <tbody id='Lb0Mk'></tbody>

                      • <i id='Lb0Mk'><tr id='Lb0Mk'><dt id='Lb0Mk'><q id='Lb0Mk'><span id='Lb0Mk'><b id='Lb0Mk'><form id='Lb0Mk'><ins id='Lb0Mk'></ins><ul id='Lb0Mk'></ul><sub id='Lb0Mk'></sub></form><legend id='Lb0Mk'></legend><bdo id='Lb0Mk'><pre id='Lb0Mk'><center id='Lb0Mk'></center></pre></bdo></b><th id='Lb0Mk'></th></span></q></dt></tr></i><div id='Lb0Mk'><tfoot id='Lb0Mk'></tfoot><dl id='Lb0Mk'><fieldset id='Lb0Mk'></fieldset></dl></div>

                        <small id='Lb0Mk'></small><noframes id='Lb0Mk'>

                      • <legend id='Lb0Mk'><style id='Lb0Mk'><dir id='Lb0Mk'><q id='Lb0Mk'></q></dir></style></legend>