NCViewerPDFSearch.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // NCViewerPDFSearch.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 23/03/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import PDFKit
  25. @available(iOS 11, *)
  26. @objc protocol NCViewerPDFSearchDelegate : class {
  27. func searchPdfSelection(_ pdfSelection: PDFSelection)
  28. }
  29. @available(iOS 11, *)
  30. class NCViewerPDFSearch: UITableViewController, UISearchBarDelegate, PDFDocumentDelegate {
  31. var searchBar = UISearchBar()
  32. var pdfDocument: PDFDocument?
  33. var searchResultArray = [PDFSelection]()
  34. weak var delegate: NCViewerPDFSearchDelegate?
  35. //MARK: - LifeCycle
  36. override func viewDidLoad() {
  37. super.viewDidLoad()
  38. searchBar.delegate = self
  39. searchBar.sizeToFit()
  40. searchBar.searchBarStyle = .minimal
  41. navigationItem.titleView = searchBar
  42. tableView.dataSource = self
  43. tableView.delegate = self
  44. tableView.register(UINib.init(nibName: "NCViewerPDFSearchCell", bundle: nil), forCellReuseIdentifier: "Cell")
  45. }
  46. override func viewWillAppear(_ animated: Bool) {
  47. super.viewWillAppear(animated)
  48. searchBar.becomeFirstResponder()
  49. }
  50. //MARK: - UITableView DataSource / Delegate
  51. override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  52. return 82
  53. }
  54. override func numberOfSections(in tableView: UITableView) -> Int {
  55. return 1
  56. }
  57. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  58. return self.searchResultArray.count
  59. }
  60. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  61. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! NCViewerPDFSearchCell
  62. let pdfSelection = searchResultArray[indexPath.row] as PDFSelection
  63. //if let pdfOutline = pdfDocument?.outlineItem(for: pdfSelection) {
  64. // cell.outlineLabel.text = pdfOutline.label
  65. //}
  66. let pdfPage = pdfSelection.pages.first
  67. cell.pageNumberLabel.text = "Page: " + (pdfPage?.label ?? "")
  68. let extendSelection = pdfSelection.copy() as! PDFSelection
  69. extendSelection.extend(atStart: 10)
  70. extendSelection.extend(atEnd: 90)
  71. extendSelection.extendForLineBoundaries()
  72. let nsRange = NSString(string: extendSelection.string!).range(of: pdfSelection.string!, options: String.CompareOptions.caseInsensitive)
  73. if nsRange.location != NSNotFound {
  74. let attributedSubString = NSAttributedString.init(string: NSString(string: extendSelection.string!).substring(with: nsRange), attributes: [NSAttributedString.Key.font : UIFont.boldSystemFont(ofSize: 17)])
  75. let attributedString = NSMutableAttributedString.init(string: extendSelection.string!)
  76. attributedString.replaceCharacters(in: nsRange, with: attributedSubString)
  77. cell.searchResultTextLabel.attributedText = attributedString
  78. }
  79. return cell
  80. }
  81. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  82. let pdfSelection = searchResultArray[indexPath.row]
  83. delegate?.searchPdfSelection(pdfSelection)
  84. dismiss(animated: true)
  85. }
  86. //MARK: - PDFSelection Delegate
  87. func didMatchString(_ instance: PDFSelection) {
  88. searchResultArray.append(instance)
  89. tableView.reloadData()
  90. }
  91. //MARK: - UIScrollView Delegate
  92. override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
  93. searchBar.resignFirstResponder()
  94. }
  95. //MARK: - UISearchBarDelegate
  96. func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
  97. searchBar.resignFirstResponder()
  98. }
  99. func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
  100. pdfDocument?.cancelFindString()
  101. navigationItem.setRightBarButton(nil, animated: false)
  102. dismiss(animated: true)
  103. }
  104. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  105. if searchText.count < 2 { return }
  106. searchResultArray.removeAll()
  107. tableView.reloadData()
  108. pdfDocument?.cancelFindString()
  109. pdfDocument?.delegate = self
  110. pdfDocument?.beginFindString(searchText, withOptions: .caseInsensitive)
  111. }
  112. func searchBarShouldBeginEditing(_ searchBar: UISearchBar) -> Bool {
  113. let cancelBarButtonItem = UIBarButtonItem.init(title: "Cancel", style: .plain, target: self, action: #selector(cancelBarButtonItemClicked))
  114. navigationItem.setRightBarButton(cancelBarButtonItem, animated: true)
  115. return true
  116. }
  117. @objc func cancelBarButtonItemClicked() {
  118. searchBarCancelButtonClicked(searchBar)
  119. }
  120. }
  121. class NCViewerPDFSearchCell: UITableViewCell {
  122. @IBOutlet weak var outlineLabel: UILabel!
  123. @IBOutlet weak var pageNumberLabel: UILabel!
  124. @IBOutlet weak var searchResultTextLabel: UILabel!
  125. override func awakeFromNib() {
  126. super.awakeFromNib()
  127. }
  128. }