CSearchSettingViewController.swift 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // CSearchSettingViewController.swift
  3. // ComPDFKit_Tools
  4. //
  5. // Copyright © 2014-2024 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // THIS SOURCE CODE AND ANY ACCOMPANYING DOCUMENTATION ARE PROTECTED BY INTERNATIONAL COPYRIGHT LAW
  8. // AND MAY NOT BE RESOLD OR REDISTRIBUTED. USAGE IS BOUND TO THE ComPDFKit LICENSE AGREEMENT.
  9. // UNAUTHORIZED REPRODUCTION OR DISTRIBUTION IS SUBJECT TO CIVIL AND CRIMINAL PENALTIES.
  10. // This notice may not be removed from this file.
  11. //
  12. import UIKit
  13. import ComPDFKit
  14. class CSearchSettingViewController: UIViewController {
  15. var isSensitive: Bool = false
  16. var isWholeWord: Bool = false
  17. private var wholeWordLabel: UILabel?
  18. private var wholeWordSwitch: UISwitch?
  19. private var sensitiveLabel: UILabel?
  20. private var sensitiveSwitch: UISwitch?
  21. private let tipView = CPDFTipsView(frame: CGRect.zero)
  22. public var callback: ((CPDFSearchOptions) -> Void)?
  23. // MARK: - Initializers
  24. init(isSensitive: Bool, isWholeWord: Bool) {
  25. super.init(nibName: nil, bundle: nil)
  26. // Initialization code
  27. self.isSensitive = isSensitive
  28. self.isWholeWord = isWholeWord
  29. }
  30. required init?(coder: NSCoder) {
  31. fatalError("init(coder:) has not been implemented")
  32. }
  33. // MARK: - UIViewController Methods
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. self.title = NSLocalizedString("Setting", comment: "")
  37. sensitiveLabel = UILabel()
  38. sensitiveLabel?.text = NSLocalizedString("Ignore Case", comment: "")
  39. sensitiveLabel?.font = UIFont.systemFont(ofSize: 15)
  40. if sensitiveLabel != nil {
  41. view.addSubview(sensitiveLabel!)
  42. }
  43. sensitiveSwitch = UISwitch()
  44. sensitiveSwitch?.addTarget(self, action: #selector(selectChange_switch(_:)), for: .valueChanged)
  45. sensitiveSwitch?.isOn = isSensitive
  46. if sensitiveSwitch != nil {
  47. view.addSubview(sensitiveSwitch!)
  48. }
  49. wholeWordLabel = UILabel()
  50. wholeWordLabel?.text = NSLocalizedString("Whole Words only", comment: "")
  51. wholeWordLabel?.font = UIFont.systemFont(ofSize: 15)
  52. if wholeWordLabel != nil {
  53. view.addSubview(wholeWordLabel!)
  54. }
  55. wholeWordSwitch = UISwitch()
  56. wholeWordSwitch?.addTarget(self, action: #selector(selectChange_switch(_:)), for: .valueChanged)
  57. wholeWordSwitch?.isOn = isWholeWord
  58. if wholeWordSwitch != nil {
  59. view.addSubview(wholeWordSwitch!)
  60. }
  61. view.backgroundColor = CPDFColorUtils.CPDFViewControllerBackgroundColor()
  62. updatePreferredContentSize(with: traitCollection)
  63. let searchBackItem = UIBarButtonItem(image: UIImage(named: "CPDFSearchImageClose", in: Bundle(for: self.classForCoder), compatibleWith: nil), style: .plain, target: self, action: #selector(buttonItemClicked_Done(_:)))
  64. self.navigationItem.rightBarButtonItems = [searchBackItem]
  65. }
  66. override func viewDidDisappear(_ animated: Bool) {
  67. super.viewDidDisappear(animated)
  68. var searchOptions: CPDFSearchOptions = CPDFSearchOptions(rawValue: 0)
  69. if sensitiveSwitch?.isOn == false {
  70. searchOptions = .caseSensitive
  71. } else {
  72. searchOptions = CPDFSearchOptions(rawValue: 0)
  73. }
  74. if wholeWordSwitch?.isOn == false {
  75. } else {
  76. searchOptions.formUnion(.matchWholeWord)
  77. }
  78. callback?(searchOptions)
  79. }
  80. override func viewWillLayoutSubviews() {
  81. super.viewWillLayoutSubviews()
  82. var top: CGFloat = 0
  83. var bottom: CGFloat = 0
  84. var left: CGFloat = 0
  85. var right: CGFloat = 0
  86. if #available(iOS 11.0, *) {
  87. bottom += self.view.safeAreaInsets.bottom
  88. top += self.view.safeAreaInsets.top
  89. left += self.view.safeAreaInsets.left
  90. right += self.view.safeAreaInsets.right
  91. }
  92. self.sensitiveLabel?.frame = CGRect(x: 25 + left, y: top+5, width: 150, height: 50)
  93. self.sensitiveSwitch?.frame = CGRect(x: self.view.frame.size.width - 75 - right, y: top+10, width: 50, height: 50)
  94. self.wholeWordLabel?.frame = CGRect(x: 25 + left, y: (self.sensitiveLabel?.frame.maxY ?? 0) + 10, width: 200, height: 50)
  95. self.wholeWordSwitch?.frame = CGRect(x: self.view.frame.size.width - 75 - right, y: (self.sensitiveLabel?.frame.maxY ?? 0) + 15, width: 50, height: 50)
  96. }
  97. override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
  98. super.willTransition(to: newCollection, with: coordinator)
  99. updatePreferredContentSize(with: newCollection)
  100. }
  101. func updatePreferredContentSize(with traitCollection: UITraitCollection) {
  102. preferredContentSize = CGSize(width: view.bounds.size.width, height: 140)
  103. }
  104. // MARK: - Action
  105. @objc func selectChange_switch(_ sender: UISwitch) {
  106. if(self.tipView.superview == nil ) {
  107. self.tipView.postAlertWithMessage(message: NSLocalizedString("Effective immediately after setting", comment: ""))
  108. self.tipView.showView(self.view.window ?? self.view)
  109. } else {
  110. self.tipView.showTip()
  111. }
  112. }
  113. @objc func buttonItemClicked_Done(_ sender: Any) {
  114. self.dismiss(animated: true)
  115. }
  116. }