CPDFNoteViewController.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // CPDFNoteViewController.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. @objc protocol CPDFNoteViewControllerDelegate: AnyObject {
  15. @objc optional func noteViewController(_ noteViewController: CPDFNoteViewController, annotStyle: CAnnotStyle)
  16. }
  17. class CPDFNoteViewController: UIViewController, UIColorPickerViewControllerDelegate, CPDFColorSelectViewDelegate, CPDFColorPickerViewDelegate {
  18. var annoStyle: CAnnotStyle?
  19. fileprivate var sampleView: CPDFSampleView?
  20. fileprivate var colorView: CPDFColorSelectView?
  21. fileprivate var colorPicker: CPDFColorPickerView?
  22. fileprivate var scrcollView: UIScrollView?
  23. fileprivate var backBtn: UIButton?
  24. fileprivate var titleLabel: UILabel?
  25. fileprivate var sampleBackgoundView: UIView?
  26. fileprivate var headerView: UIView?
  27. weak var delegate: CPDFNoteViewControllerDelegate?
  28. // MARK: - Initializers
  29. init(style: CAnnotStyle) {
  30. self.annoStyle = style
  31. super.init(nibName: nil, bundle: nil)
  32. }
  33. required init?(coder aDecoder: NSCoder) {
  34. fatalError("init(coder:) has not been implemented")
  35. }
  36. // MARK: - ViewController Methods
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. // Common initialization code
  40. self.headerView = UIView()
  41. self.headerView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  42. self.headerView?.layer.borderWidth = 1.0
  43. self.headerView?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  44. if headerView != nil {
  45. self.view.addSubview(self.headerView!)
  46. }
  47. self.titleLabel = UILabel()
  48. self.titleLabel?.autoresizingMask = .flexibleRightMargin
  49. self.titleLabel?.textAlignment = .center
  50. self.titleLabel?.font = UIFont.systemFont(ofSize: 20)
  51. self.titleLabel?.text = NSLocalizedString("Note", comment: "")
  52. self.titleLabel?.adjustsFontSizeToFitWidth = true
  53. if titleLabel != nil {
  54. self.headerView?.addSubview(self.titleLabel!)
  55. }
  56. self.scrcollView = UIScrollView()
  57. self.scrcollView?.isScrollEnabled = true
  58. if scrcollView != nil {
  59. self.view.addSubview(self.scrcollView!)
  60. }
  61. self.backBtn = UIButton()
  62. self.backBtn?.autoresizingMask = .flexibleLeftMargin
  63. self.backBtn?.setImage(UIImage(named: "CPDFAnnotationBaseImageBack", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  64. self.backBtn?.addTarget(self, action: #selector(buttonItemClicked_back(_:)), for: .touchUpInside)
  65. if backBtn != nil {
  66. self.headerView?.addSubview(self.backBtn!)
  67. }
  68. self.sampleBackgoundView = UIView()
  69. self.sampleBackgoundView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  70. self.sampleBackgoundView?.layer.borderWidth = 1.0
  71. self.sampleBackgoundView?.backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  72. if sampleBackgoundView != nil {
  73. self.headerView?.addSubview(self.sampleBackgoundView!)
  74. }
  75. self.sampleView = CPDFSampleView()
  76. self.sampleView?.backgroundColor = UIColor.white
  77. self.sampleView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  78. self.sampleView?.layer.borderWidth = 1.0
  79. self.sampleView?.autoresizingMask = .flexibleRightMargin
  80. if sampleView != nil {
  81. self.sampleBackgoundView?.addSubview(self.sampleView!)
  82. }
  83. self.colorView = CPDFColorSelectView()
  84. self.colorView?.delegate = self
  85. self.colorView?.autoresizingMask = .flexibleWidth
  86. if colorView != nil {
  87. self.scrcollView?.addSubview(self.colorView!)
  88. }
  89. self.view.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  90. self.updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  91. }
  92. override func viewWillLayoutSubviews() {
  93. super.viewWillLayoutSubviews()
  94. titleLabel?.frame = CGRect(x: (view.frame.size.width - 120) / 2, y: 5, width: 120, height: 50)
  95. scrcollView?.frame = CGRect(x: 0, y: 170, width: view.frame.size.width, height: 210)
  96. headerView?.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 170)
  97. scrcollView?.contentSize = CGSize(width: view.frame.size.width, height: 330)
  98. sampleBackgoundView?.frame = CGRect(x: 0, y: 50, width: view.bounds.size.width, height: 120)
  99. sampleView?.frame = CGRect(x: (view.frame.size.width - 300) / 2, y: 15, width: 300, height: (sampleBackgoundView?.bounds.size.height ?? 0) - 30)
  100. if #available(iOS 11.0, *) {
  101. colorPicker?.frame = CGRect(x: view.safeAreaInsets.left, y: 0, width: view.frame.size.width - view.safeAreaInsets.left - view.safeAreaInsets.right, height: view.frame.size.height)
  102. colorView?.frame = CGRect(x: view.safeAreaInsets.left, y: 0, width: view.frame.size.width - view.safeAreaInsets.left - view.safeAreaInsets.right, height: 90)
  103. backBtn?.frame = CGRect(x: view.frame.size.width - 60 - view.safeAreaInsets.right, y: 5, width: 50, height: 50)
  104. } else {
  105. colorView?.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 90)
  106. backBtn?.frame = CGRect(x: view.frame.size.width - 60, y: 5, width: 50, height: 50)
  107. }
  108. }
  109. override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
  110. super.willTransition(to: newCollection, with: coordinator)
  111. updatePreferredContentSizeWithTraitCollection(traitCollection: newCollection)
  112. }
  113. override func viewWillAppear(_ animated: Bool) {
  114. sampleView?.color = annoStyle?.color
  115. sampleView?.selecIndex = .note
  116. sampleView?.opcity = annoStyle?.opacity ?? 1.0
  117. sampleView?.setNeedsDisplay()
  118. colorView?.selectedColor = annoStyle?.color
  119. }
  120. // MARK: - Protect Methods
  121. func updatePreferredContentSizeWithTraitCollection(traitCollection: UITraitCollection) {
  122. if self.colorPicker?.superview != nil {
  123. let currentDevice = UIDevice.current
  124. if currentDevice.userInterfaceIdiom == .pad {
  125. // This is an iPad
  126. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: 320)
  127. } else {
  128. // This is an iPhone or iPod touch
  129. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: 320)
  130. }
  131. } else {
  132. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? 320 : 320)
  133. }
  134. }
  135. // MARK: - Action
  136. @objc func buttonItemClicked_back(_ button: UIButton) {
  137. dismiss(animated: true)
  138. }
  139. // MARK: - CPDFColorSelectViewDelegate
  140. func selectColorView(_ select: CPDFColorSelectView) {
  141. if #available(iOS 14.0, *) {
  142. let picker = UIColorPickerViewController()
  143. picker.delegate = self
  144. self.present(picker, animated: true, completion: nil)
  145. } else {
  146. self.colorPicker = CPDFColorPickerView(frame: view.frame)
  147. self.colorPicker?.delegate = self
  148. self.colorView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  149. self.colorPicker?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  150. if colorPicker != nil {
  151. self.view.addSubview(self.colorPicker!)
  152. }
  153. updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  154. }
  155. }
  156. func selectColorView(_ select: CPDFColorSelectView, color: UIColor) {
  157. sampleView?.color = color
  158. annoStyle?.setColor(color)
  159. sampleView?.setNeedsDisplay()
  160. if annoStyle != nil {
  161. delegate?.noteViewController?(self, annotStyle: annoStyle!)
  162. }
  163. }
  164. // MARK: - CPDFColorPickerViewDelegate
  165. func pickerView(_ colorPickerView: CPDFColorPickerView, color: UIColor) {
  166. sampleView?.color = color
  167. annoStyle?.setColor(color)
  168. sampleView?.setNeedsDisplay()
  169. if annoStyle != nil {
  170. delegate?.noteViewController?(self, annotStyle: annoStyle!)
  171. }
  172. }
  173. // MARK: - UIColorPickerViewControllerDelegate
  174. @available(iOS 14.0, *)
  175. func colorPickerViewControllerDidFinish(_ viewController: UIColorPickerViewController) {
  176. sampleView?.color = viewController.selectedColor
  177. annoStyle?.setColor(sampleView?.color)
  178. sampleView?.setNeedsDisplay()
  179. if annoStyle != nil {
  180. delegate?.noteViewController?(self, annotStyle: annoStyle!)
  181. }
  182. }
  183. }