CPDFAnnotationBaseViewController.swift 11 KB

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