Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.41 KB | None | 0 0
  1. import UIKit
  2. import PDFKit
  3.  
  4. protocol SearchTableViewControllerDelegate: class {
  5.     func searchTableViewController(_ searchTableViewController: SearchTableViewController, didSelectSerchResult selection: PDFSelection)
  6. }
  7.  
  8. class SearchTableViewController: UITableViewController {
  9.  
  10.     open var pdfDocument: PDFDocument?
  11.     weak var delegate: SearchTableViewControllerDelegate?
  12.  
  13.     var searchBar = UISearchBar()
  14.     var searchResults = [PDFSelection]()
  15.  
  16.     override func viewDidLoad() {
  17.         super.viewDidLoad()
  18.  
  19.         tableView.rowHeight = 150
  20.  
  21.         searchBar.delegate = self
  22.         searchBar.showsCancelButton = true
  23.         searchBar.searchBarStyle = .minimal
  24.         navigationItem.titleView = searchBar
  25.  
  26.         navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .cancel,
  27.                                                            target: self,
  28.                                                            action: #selector(closeBtnClick))
  29.  
  30.         tableView.register(UINib(nibName: "SearchViewCell", bundle: nil), forCellReuseIdentifier: "SearchViewCell")
  31.  
  32.     }
  33.  
  34.     @objc func closeBtnClick(sender: UIBarButtonItem) {
  35.         dismiss(animated: false, completion: nil)
  36.     }
  37.  
  38.     override func viewWillAppear(_ animated: Bool) {
  39.         super.viewWillAppear(animated)
  40.         searchBar.becomeFirstResponder()
  41.     }
  42.  
  43.     override func didReceiveMemoryWarning() {
  44.         super.didReceiveMemoryWarning()
  45.         // Dispose of any resources that can be recreated.
  46.     }
  47.  
  48.     // MARK: - Table view data source
  49.     override func numberOfSections(in tableView: UITableView) -> Int {
  50.         // #warning Incomplete implementation, return the number of sections
  51.         return 1
  52.     }
  53.  
  54.     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  55.         // #warning Incomplete implementation, return the number of rows
  56.         return searchResults.count
  57.     }
  58.  
  59.     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  60.         let cell = tableView.dequeueReusableCell(withIdentifier: "SearchViewCell", for: indexPath) as! SearchViewCell
  61.  
  62.         let selection = searchResults[indexPath.row]
  63.         let page = selection.pages[0]
  64.         let outline = pdfDocument?.outlineItem(for: selection)
  65.  
  66.         let outlintstr = outline?.label ?? ""
  67.         let pagestr = page.label ?? ""
  68.         let txt = outlintstr + " ้กต็ :  " + pagestr
  69.         cell.destinationLabel.text = ""
  70.  
  71.         let extendSelection = selection.copy() as! PDFSelection
  72.         extendSelection.extend(atStart: 10)
  73.         extendSelection.extend(atEnd: 90)
  74.         extendSelection.extendForLineBoundaries()
  75.  
  76.         let range = (extendSelection.string! as NSString).range(of: selection.string!, options: .caseInsensitive)
  77.         let attrstr = NSMutableAttributedString(string: extendSelection.string!)
  78.         attrstr.addAttribute(NSAttributedStringKey.backgroundColor, value: UIColor.yellow, range: range)
  79.  
  80.         cell.resultTextLabel.attributedText = attrstr
  81.  
  82.         return cell
  83.     }
  84.  
  85.     override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  86.  
  87.         let selection = searchResults[indexPath.row]
  88.         delegate?.searchTableViewController(self, didSelectSerchResult: selection)
  89.         dismiss(animated: false, completion: nil)
  90.     }
  91.  
  92.     override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  93.         searchBar.resignFirstResponder()
  94.     }
  95. }
  96.  
  97. extension SearchTableViewController: UISearchBarDelegate {
  98.     func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  99.         searchBar.resignFirstResponder()
  100.     }
  101.  
  102.     func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  103.         pdfDocument?.cancelFindString()
  104.         dismiss(animated: false, completion: nil)
  105.     }
  106.  
  107.     func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  108. //        if searchText.count < 2 {
  109. //            return
  110. //        }
  111.  
  112.         searchResults.removeAll()
  113.         tableView.reloadData()
  114.         pdfDocument?.cancelFindString()
  115.         pdfDocument?.delegate = self
  116.         pdfDocument?.beginFindString(searchText, withOptions: .caseInsensitive)
  117.     }
  118. }
  119.  
  120. extension SearchTableViewController: PDFDocumentDelegate {
  121.     func didMatchString(_ instance: PDFSelection) {
  122.         searchResults.append(instance)
  123.         tableView.reloadData()
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement