CPDFInkTopToolBar.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // CPDFInkTopToolBar.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. public enum CPDFInkTopToolBarSelect: Int {
  14. case setting = 0
  15. case erase
  16. case undo
  17. case redo
  18. case clear
  19. case save
  20. }
  21. @objc public protocol CPDFInkTopToolBarDelegate: AnyObject {
  22. @objc optional func inkTopToolBar(_ inkTopToolBar: CPDFInkTopToolBar, tag: Int, isSelect: Bool)
  23. }
  24. public class CPDFInkTopToolBar: UIView {
  25. public weak var delegate: CPDFInkTopToolBarDelegate?
  26. public var buttonArray: [UIButton] = []
  27. // MARK: - Initializers
  28. public override init(frame: CGRect) {
  29. super.init(frame: frame)
  30. self.layer.borderColor = UIColor(red: 170.0/255.0, green: 170.0/255.0, blue: 170.0/255.0, alpha: 1.0).cgColor
  31. self.layer.borderWidth = 1.0
  32. self.layer.cornerRadius = 5.0
  33. let width = self.bounds.size.width / 6
  34. let height = self.bounds.size.height
  35. self.buttonArray = []
  36. let settingButton = UIButton(type: .custom)
  37. settingButton.tag = CPDFInkTopToolBarSelect.setting.rawValue
  38. settingButton.frame = CGRect(x: 0, y: 0, width: width, height: height)
  39. settingButton.setImage(UIImage(named: "CPDFInkImageSetting", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
  40. settingButton.addTarget(self, action: #selector(buttonItemClicked_Switch(_:)), for: .touchUpInside)
  41. self.addSubview(settingButton)
  42. self.buttonArray.append(settingButton)
  43. let eraseButton = UIButton(type: .custom)
  44. eraseButton.tag = CPDFInkTopToolBarSelect.erase.rawValue
  45. eraseButton.frame = CGRect(x: width, y: 0, width: width, height: height)
  46. eraseButton.setImage(UIImage(named: "CPDFInkImageEraer", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
  47. eraseButton.addTarget(self, action: #selector(buttonItemClicked_Switch(_:)), for: .touchUpInside)
  48. self.addSubview(eraseButton)
  49. self.buttonArray.append(eraseButton)
  50. let undoButton = UIButton(type: .custom)
  51. undoButton.tag = CPDFInkTopToolBarSelect.undo.rawValue
  52. undoButton.frame = CGRect(x: width * 2, y: 0, width: width, height: height)
  53. undoButton.setImage(UIImage(named: "CPDFInkImageUndo", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
  54. undoButton.addTarget(self, action: #selector(buttonItemClicked_Switch(_:)), for: .touchUpInside)
  55. self.addSubview(undoButton)
  56. self.buttonArray.append(undoButton)
  57. let redoButton = UIButton(type: .custom)
  58. redoButton.tag = CPDFInkTopToolBarSelect.redo.rawValue
  59. redoButton.frame = CGRect(x: width * 3, y: 0, width: width, height: height)
  60. redoButton.setImage(UIImage(named: "CPDFInkImageRedo", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
  61. redoButton.addTarget(self, action: #selector(buttonItemClicked_Switch(_:)), for: .touchUpInside)
  62. self.addSubview(redoButton)
  63. self.buttonArray.append(redoButton)
  64. let clearButton = UIButton(type: .system)
  65. clearButton.tag = CPDFInkTopToolBarSelect.clear.rawValue
  66. clearButton.frame = CGRect(x: 4 * width, y: 0, width: width, height: height)
  67. clearButton.setTitle(NSLocalizedString("Clear", comment: ""), for: .normal)
  68. clearButton.setTitleColor(CPDFColorUtils.CPageEditToolbarFontColor(), for: .normal)
  69. clearButton.titleLabel?.font = UIFont.systemFont(ofSize: 15.0)
  70. clearButton.addTarget(self, action: #selector(buttonItemClicked_Switch(_:)), for: .touchUpInside)
  71. self.addSubview(clearButton)
  72. self.buttonArray.append(clearButton)
  73. let view = UIView(frame: CGRect(x: 4 * width, y: 10, width: 1, height: height - 20))
  74. view.backgroundColor = UIColor(red: 0.0/255.0, green: 0.0/255.0, blue: 0.0/255.0, alpha: 0.1)
  75. self.addSubview(view)
  76. let saveButton = UIButton(type: .system)
  77. saveButton.tag = CPDFInkTopToolBarSelect.save.rawValue
  78. saveButton.frame = CGRect(x: 5 * width, y: 0, width: width, height: height)
  79. saveButton.setTitle(NSLocalizedString("Save", comment: ""), for: .normal)
  80. saveButton.setTitleColor(UIColor(red: 20.0/255.0, green: 96.0/255.0, blue: 243.0/255.0, alpha: 1.0), for: .normal)
  81. saveButton.titleLabel?.font = UIFont.systemFont(ofSize: 15.0)
  82. saveButton.addTarget(self, action: #selector(buttonItemClicked_Switch(_:)), for: .touchUpInside)
  83. self.addSubview(saveButton)
  84. self.buttonArray.append(saveButton)
  85. self.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  86. }
  87. required init?(coder: NSCoder) {
  88. fatalError("init(coder:) has not been implemented")
  89. }
  90. // MARK: - Action
  91. @objc func buttonItemClicked_Switch(_ button: UIButton) {
  92. for j in 0..<buttonArray.count {
  93. (buttonArray[j] ).backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  94. }
  95. buttonArray[button.tag].backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  96. switch button.tag {
  97. case CPDFInkTopToolBarSelect.setting.rawValue:
  98. delegate?.inkTopToolBar?(self, tag: CPDFInkTopToolBarSelect(rawValue: button.tag)?.rawValue ?? 0, isSelect: button.isSelected)
  99. case CPDFInkTopToolBarSelect.erase.rawValue:
  100. button.isSelected = !button.isSelected
  101. if !button.isSelected {
  102. button.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  103. }
  104. delegate?.inkTopToolBar?(self, tag: CPDFInkTopToolBarSelect(rawValue: button.tag)?.rawValue ?? 0, isSelect: button.isSelected)
  105. case CPDFInkTopToolBarSelect.undo.rawValue:
  106. delegate?.inkTopToolBar?(self, tag: CPDFInkTopToolBarSelect(rawValue: button.tag)?.rawValue ?? 0, isSelect: button.isSelected)
  107. (buttonArray[button.tag]).backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  108. case CPDFInkTopToolBarSelect.redo.rawValue:
  109. delegate?.inkTopToolBar?(self, tag: CPDFInkTopToolBarSelect(rawValue: button.tag)?.rawValue ?? 0, isSelect: button.isSelected)
  110. (buttonArray[button.tag]).backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  111. case CPDFInkTopToolBarSelect.clear.rawValue:
  112. delegate?.inkTopToolBar?(self, tag: CPDFInkTopToolBarSelect(rawValue: button.tag)?.rawValue ?? 0, isSelect: button.isSelected)
  113. removeFromSuperview()
  114. case CPDFInkTopToolBarSelect.save.rawValue:
  115. delegate?.inkTopToolBar?(self, tag: CPDFInkTopToolBarSelect(rawValue: button.tag)?.rawValue ?? 0, isSelect: button.isSelected)
  116. removeFromSuperview()
  117. default:
  118. break
  119. }
  120. }
  121. }