CPDFNoteOpenViewController.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // CPDFNoteOpenViewController.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 public protocol CPDFNoteOpenViewControllerDelegate: AnyObject {
  15. @objc optional func getNoteOpenViewController(_ noteOpenVC: CPDFNoteOpenViewController, content: String, isDelete: Bool)
  16. }
  17. public class CPDFNoteOpenViewController: UIViewController {
  18. public weak var delegate: CPDFNoteOpenViewControllerDelegate?
  19. public var annotation: CPDFAnnotation?
  20. private var noteTextView: UITextView?
  21. private var textViewContent: String?
  22. private var contentView: UIView?
  23. // MARK: - Initializers
  24. public init(annotation: CPDFAnnotation) {
  25. self.annotation = annotation
  26. super.init(nibName: nil, bundle: nil)
  27. }
  28. required init?(coder aDecoder: NSCoder) {
  29. fatalError("init(coder:) has not been implemented")
  30. }
  31. // MARK: - ViewController Methods
  32. public override func viewDidLoad() {
  33. super.viewDidLoad()
  34. self.view.backgroundColor = CPDFColorUtils.CNoteOpenBackgooundColor()
  35. noteTextView = UITextView(frame: CGRect(x: 10, y: 10, width: self.view.bounds.size.width, height: self.view.bounds.size.height - 60))
  36. noteTextView?.delegate = self
  37. noteTextView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  38. noteTextView?.backgroundColor = UIColor.clear
  39. noteTextView?.font = UIFont.systemFont(ofSize: 14)
  40. noteTextView?.textAlignment = .left
  41. noteTextView?.textColor = UIColor.black
  42. if noteTextView != nil {
  43. self.view.addSubview(noteTextView!)
  44. }
  45. noteTextView?.text = self.textViewContent
  46. let deleteButton = UIButton(type: .custom)
  47. deleteButton.setImage(UIImage(named: "CPDFNoteContentImageNameDelete", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  48. deleteButton.sizeToFit()
  49. var frame = deleteButton.frame
  50. frame.origin.x = 10
  51. frame.origin.y = self.view.bounds.size.height - deleteButton.bounds.size.height - 10
  52. deleteButton.frame = frame
  53. deleteButton.autoresizingMask = .flexibleTopMargin
  54. deleteButton.addTarget(self, action: #selector(buttonItemClicked_Delete(_:)), for: .touchUpInside)
  55. self.view.addSubview(deleteButton)
  56. let saveButton = UIButton(type: .custom)
  57. saveButton.setImage(UIImage(named: "CPDFNoteContentImageNameSave", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  58. saveButton.sizeToFit()
  59. frame = saveButton.frame
  60. frame.origin.x = self.view.bounds.size.width - saveButton.bounds.size.width - 10
  61. frame.origin.y = self.view.bounds.size.height - saveButton.bounds.size.height - 10
  62. saveButton.frame = frame
  63. saveButton.autoresizingMask = [.flexibleLeftMargin, .flexibleTopMargin]
  64. saveButton.addTarget(self, action: #selector(buttonItemClicked_Save(_:)), for: .touchUpInside)
  65. self.view.addSubview(saveButton)
  66. self.noteTextView?.text = self.annotation?.contents ?? ""
  67. }
  68. public override func viewDidAppear(_ animated: Bool) {
  69. super.viewDidAppear(animated)
  70. noteTextView?.becomeFirstResponder()
  71. }
  72. public override func viewWillDisappear(_ animated: Bool) {
  73. super.viewWillDisappear(animated)
  74. if ((noteTextView?.isFirstResponder) != nil) {
  75. noteTextView?.resignFirstResponder()
  76. }
  77. }
  78. public func showViewController(_ viewController: UIViewController, inRect rect: CGRect) {
  79. // Implementation for showing view controller in rect
  80. preferredContentSize = CGSize(width: 280, height: 305)
  81. modalPresentationStyle = .popover
  82. let popVC = popoverPresentationController
  83. popVC?.delegate = self
  84. popVC?.sourceRect = rect
  85. popVC?.sourceView = viewController.view
  86. popVC?.canOverlapSourceViewRect = true
  87. popVC?.popoverBackgroundViewClass = UIPopBackgroundView.classForCoder() as? UIPopoverBackgroundViewMethods.Type
  88. popVC?.permittedArrowDirections = .unknown
  89. viewController.present(self, animated: true)
  90. }
  91. // MARK: - Action
  92. @objc func buttonItemClicked_Delete(_ button: UIButton) {
  93. dismiss(animated: true)
  94. delegate?.getNoteOpenViewController?(self, content: noteTextView?.text ?? "", isDelete: true)
  95. }
  96. @objc func buttonItemClicked_Save(_ button: UIButton) {
  97. dismiss(animated: true)
  98. delegate?.getNoteOpenViewController?(self, content: noteTextView?.text ?? "", isDelete: false)
  99. }
  100. }
  101. extension CPDFNoteOpenViewController: UIPopoverPresentationControllerDelegate, UITextViewDelegate {
  102. // Implement the methods for UIPopoverPresentationControllerDelegate and UITextViewDelegate here
  103. // MARK: - UIPopoverPresentationControllerDelegate
  104. public func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
  105. return UIModalPresentationStyle.none
  106. }
  107. public func popoverPresentationControllerDidDismissPopover(_ popoverPresentationController: UIPopoverPresentationController) {
  108. delegate?.getNoteOpenViewController?(self, content: noteTextView?.text ?? "", isDelete: false)
  109. }
  110. }