CStampShapView.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // CStampShapView.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. @objc protocol CStampShapViewDelegate: AnyObject {
  14. @objc optional func stampShapView(_ stampShapView: CStampShapView, tag: Int)
  15. }
  16. class CStampShapView: UIView {
  17. weak var delegate: CStampShapViewDelegate?
  18. private var titleLabel: UILabel?
  19. private var centerButton: UIButton?
  20. private var leftButton: UIButton?
  21. private var rightButton: UIButton?
  22. private var noneButton: UIButton?
  23. private var shapeView: UIView?
  24. private var buttonArray: [UIButton] = []
  25. // MARK: - Initializers
  26. override init(frame: CGRect) {
  27. super.init(frame: frame)
  28. self.titleLabel = UILabel()
  29. self.titleLabel?.text = NSLocalizedString("Style", comment: "")
  30. self.titleLabel?.autoresizingMask = .flexibleRightMargin
  31. self.titleLabel?.textColor = UIColor.gray
  32. self.titleLabel?.font = UIFont.systemFont(ofSize: 12.0)
  33. if titleLabel != nil {
  34. self.addSubview(self.titleLabel!)
  35. }
  36. self.shapeView = UIView(frame: CGRect(x: 0, y: 30, width: self.bounds.size.width, height: 60))
  37. if shapeView != nil {
  38. self.addSubview(self.shapeView!)
  39. }
  40. self.buttonArray = []
  41. self.centerButton = UIButton()
  42. self.centerButton?.setImage(UIImage(named: "CPDFStampTextImageCenter", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  43. self.centerButton?.addTarget(self, action: #selector(buttonItemClicked_select(_:)), for: .touchUpInside)
  44. self.centerButton?.tag = 0
  45. self.centerButton?.layer.cornerRadius = 5.0
  46. self.centerButton?.layer.masksToBounds = true
  47. if centerButton != nil {
  48. self.shapeView?.addSubview(self.centerButton!)
  49. self.buttonArray.append(self.centerButton!)
  50. }
  51. self.centerButton?.backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  52. self.leftButton = UIButton()
  53. self.leftButton?.setImage(UIImage(named: "CPDFStampTextImageLeft", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  54. self.leftButton?.addTarget(self, action: #selector(buttonItemClicked_select(_:)), for: .touchUpInside)
  55. self.leftButton?.tag = 1
  56. self.leftButton?.layer.cornerRadius = 5.0
  57. self.leftButton?.layer.masksToBounds = true
  58. if leftButton != nil {
  59. self.shapeView?.addSubview(self.leftButton!)
  60. self.buttonArray.append(self.leftButton!)
  61. }
  62. self.rightButton = UIButton()
  63. self.rightButton?.setImage(UIImage(named: "CPDFStampTextImageRight", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  64. self.rightButton?.addTarget(self, action: #selector(buttonItemClicked_select(_:)), for: .touchUpInside)
  65. self.rightButton?.tag = 2
  66. self.rightButton?.layer.cornerRadius = 5.0
  67. self.rightButton?.layer.masksToBounds = true
  68. if rightButton != nil {
  69. self.shapeView?.addSubview(self.rightButton!)
  70. self.buttonArray.append(self.rightButton!)
  71. }
  72. self.noneButton = UIButton()
  73. self.noneButton?.setImage(UIImage(named: "CPDFStampTextImageNone", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  74. self.noneButton?.addTarget(self, action: #selector(buttonItemClicked_select(_:)), for: .touchUpInside)
  75. self.noneButton?.tag = 3
  76. self.noneButton?.layer.cornerRadius = 5.0
  77. self.noneButton?.layer.masksToBounds = true
  78. if noneButton != nil {
  79. self.shapeView?.addSubview(self.noneButton!)
  80. self.buttonArray.append(self.noneButton!)
  81. }
  82. }
  83. required init?(coder aDecoder: NSCoder) {
  84. fatalError("init(coder:) has not been implemented")
  85. }
  86. override func layoutSubviews() {
  87. super.layoutSubviews()
  88. self.titleLabel?.frame = CGRect(x: 20, y: 0, width: 50, height: self.bounds.size.height/3)
  89. self.shapeView?.frame = CGRect(x: 0, y: self.bounds.size.height/3, width: self.bounds.size.width, height: (self.bounds.size.height/3)*2)
  90. self.centerButton?.frame = CGRect(x: (self.shapeView!.bounds.size.width - (44*4))/5, y: (self.shapeView!.bounds.size.height-44)/2, width: 44, height: 44)
  91. self.leftButton?.frame = CGRect(x: ((self.shapeView!.bounds.size.width - (44*4))/5)*2 + 44, y: (self.shapeView!.bounds.size.height-44)/2, width: 44, height: 44)
  92. let withs = (self.shapeView!.bounds.size.width - 44*4)/5
  93. self.rightButton?.frame = CGRect(x: withs*3 + 44*2, y: (self.shapeView!.bounds.size.height-44)/2, width: 44, height: 44)
  94. self.noneButton?.frame = CGRect(x: withs*4 + 44*3, y: (self.shapeView!.bounds.size.height-44)/2, width: 44, height: 44)
  95. }
  96. // MARK: - Action
  97. @objc func buttonItemClicked_select(_ button: UIButton) {
  98. // Handle button click event
  99. for i in 0..<buttonArray.count {
  100. buttonArray[i].backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  101. }
  102. buttonArray[button.tag].backgroundColor = CPDFColorUtils.CAnnotationBarSelectBackgroundColor()
  103. delegate?.stampShapView?(self, tag: button.tag)
  104. }
  105. }