CStampTextViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. //
  2. // CStampTextViewController.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 CStampTextViewControllerDelegate: AnyObject {
  14. @objc optional func stampTextViewController(_ stampTextViewController: CStampTextViewController, dictionary: NSDictionary)
  15. }
  16. class CStampTextViewController: UIViewController, UITextFieldDelegate, CStampColorSelectViewDelegate, CStampShapViewDelegate {
  17. weak var delegate: CStampTextViewControllerDelegate?
  18. // Your other properties and methods
  19. private var preView: CStampPreview?
  20. private var stampTextField: UITextField?
  21. private var haveDateSwitch: UISwitch?
  22. private var haveTimeSwitch: UISwitch?
  23. private var doneBtn: UIButton?
  24. private var titleLabel: UILabel?
  25. private var cancelBtn: UIButton?
  26. private var dateLabel: UILabel?
  27. private var timeLabel: UILabel?
  28. private var colorView: CStampColorSelectView?
  29. private var stampShapeViw: CStampShapView?
  30. private var textStampColorStyle: TextStampColorType = .black
  31. private var textStampStyle: TextStampType = .left
  32. private var scrcollView: UIScrollView?
  33. override func viewDidLoad() {
  34. super.viewDidLoad()
  35. self.titleLabel = UILabel()
  36. self.titleLabel?.autoresizingMask = [.flexibleRightMargin]
  37. self.titleLabel?.text = NSLocalizedString("Create Stamp", comment: "")
  38. self.titleLabel?.textAlignment = .center
  39. self.titleLabel?.font = UIFont.systemFont(ofSize: 20)
  40. self.titleLabel?.adjustsFontSizeToFitWidth = true
  41. if titleLabel != nil {
  42. self.view.addSubview(self.titleLabel!)
  43. }
  44. self.doneBtn = UIButton()
  45. self.doneBtn?.autoresizingMask = [.flexibleRightMargin]
  46. self.doneBtn?.setTitle(NSLocalizedString("Save", comment: ""), for: .normal)
  47. self.doneBtn?.setTitleColor(.blue, for: .normal)
  48. self.doneBtn?.addTarget(self, action: #selector(buttonItemClicked_done), for: .touchUpInside)
  49. if doneBtn != nil {
  50. self.view.addSubview(self.doneBtn!)
  51. }
  52. self.scrcollView = UIScrollView()
  53. self.scrcollView?.isScrollEnabled = true
  54. self.scrcollView?.backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  55. if scrcollView != nil {
  56. self.view.addSubview(self.scrcollView!)
  57. }
  58. self.cancelBtn = UIButton()
  59. self.cancelBtn?.autoresizingMask = [.flexibleLeftMargin]
  60. self.cancelBtn?.titleLabel?.adjustsFontSizeToFitWidth = true
  61. self.cancelBtn?.setTitle(NSLocalizedString("Cancel", comment: ""), for: .normal)
  62. self.cancelBtn?.setTitleColor(.black, for: .normal)
  63. self.cancelBtn?.addTarget(self, action: #selector(buttonItemClicked_cancel), for: .touchUpInside)
  64. if cancelBtn != nil {
  65. self.view.addSubview(self.cancelBtn!)
  66. }
  67. self.preView = CStampPreview()
  68. self.preView?.backgroundColor = .clear
  69. self.preView?.color = .black
  70. self.preView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  71. self.preView?.layer.borderWidth = 1.0
  72. self.preView?.autoresizingMask = [.flexibleRightMargin]
  73. if preView != nil {
  74. self.scrcollView?.addSubview(self.preView!)
  75. }
  76. self.stampTextField = UITextField()
  77. self.stampTextField?.contentVerticalAlignment = .center
  78. self.stampTextField?.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 8, height: 0))
  79. self.stampTextField?.leftViewMode = .always
  80. self.stampTextField?.rightView = UIView(frame: CGRect(x: 0, y: 0, width: 8, height: 0))
  81. self.stampTextField?.rightViewMode = .always
  82. self.stampTextField?.returnKeyType = .done
  83. self.stampTextField?.clearButtonMode = .whileEditing
  84. self.stampTextField?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  85. self.stampTextField?.layer.borderWidth = 1.0
  86. self.stampTextField?.delegate = self
  87. self.stampTextField?.borderStyle = .none
  88. self.stampTextField?.addTarget(self, action: #selector(textFieldEditChange), for: .editingChanged)
  89. self.stampTextField?.placeholder = NSLocalizedString("Text", comment: "")
  90. if stampTextField != nil {
  91. self.scrcollView?.addSubview(self.stampTextField!)
  92. }
  93. self.colorView = CStampColorSelectView.init(frame: CGRect.zero)
  94. self.colorView?.delegate = self
  95. self.colorView?.autoresizingMask = [.flexibleWidth]
  96. self.colorView?.selectedColor = .black
  97. if colorView != nil {
  98. self.scrcollView?.addSubview(self.colorView!)
  99. }
  100. self.stampShapeViw = CStampShapView.init(frame: CGRect.zero)
  101. self.stampShapeViw?.delegate = self
  102. self.stampShapeViw?.autoresizingMask = [.flexibleWidth]
  103. if stampShapeViw != nil {
  104. self.scrcollView?.addSubview(self.stampShapeViw!)
  105. }
  106. self.dateLabel = UILabel()
  107. self.dateLabel?.text = NSLocalizedString("Date", comment: "")
  108. self.dateLabel?.textColor = .gray
  109. self.dateLabel?.font = UIFont.systemFont(ofSize: 12.0)
  110. if dateLabel != nil {
  111. self.scrcollView?.addSubview(self.dateLabel!)
  112. }
  113. self.haveDateSwitch = UISwitch()
  114. self.haveDateSwitch?.setOn(false, animated: false)
  115. self.haveDateSwitch?.autoresizingMask = [.flexibleLeftMargin]
  116. self.haveDateSwitch?.addTarget(self, action: #selector(switchChange_date), for: .valueChanged)
  117. if haveDateSwitch != nil {
  118. self.scrcollView?.addSubview(self.haveDateSwitch!)
  119. }
  120. self.timeLabel = UILabel()
  121. self.timeLabel?.text = NSLocalizedString("Time", comment: "")
  122. self.timeLabel?.textColor = .gray
  123. self.timeLabel?.font = UIFont.systemFont(ofSize: 12.0)
  124. self.timeLabel?.autoresizingMask = [.flexibleRightMargin]
  125. if timeLabel != nil {
  126. self.scrcollView?.addSubview(self.timeLabel!)
  127. }
  128. self.haveTimeSwitch = UISwitch()
  129. self.haveTimeSwitch?.setOn(false, animated: false)
  130. self.haveTimeSwitch?.autoresizingMask = [.flexibleLeftMargin]
  131. self.haveTimeSwitch?.addTarget(self, action: #selector(switchChange_time), for: .valueChanged)
  132. if haveTimeSwitch != nil {
  133. self.scrcollView?.addSubview(self.haveTimeSwitch!)
  134. }
  135. view.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  136. updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  137. }
  138. override func viewWillLayoutSubviews() {
  139. super.viewWillLayoutSubviews()
  140. titleLabel?.frame = CGRect(x: (view.frame.size.width - 120)/2, y: 5, width: 120, height: 50)
  141. scrcollView?.frame = CGRect(x: 0, y: 50, width: view.frame.size.width, height: 560)
  142. scrcollView?.contentSize = CGSize(width: view.frame.size.width, height: 700)
  143. preView?.frame = CGRect(x: (view.frame.size.width - 350)/2, y: 15, width: 350, height: 120)
  144. stampTextField?.frame = CGRect(x: (view.frame.size.width - 350)/2, y: 150, width: 350, height: 30)
  145. if #available(iOS 11.0, *) {
  146. doneBtn?.frame = CGRect(x: view.frame.size.width - 60 - view.safeAreaInsets.right, y: 5, width: 50, height: 50)
  147. cancelBtn?.frame = CGRect(x: view.safeAreaInsets.left + 10, y: 5, width: 50, height: 50)
  148. stampShapeViw?.frame = CGRect(x: view.safeAreaInsets.left, y: 180, width: view.frame.size.width - view.safeAreaInsets.left - view.safeAreaInsets.right, height: 90)
  149. colorView?.frame = CGRect(x: view.safeAreaInsets.left, y: 270, width: view.frame.size.width - view.safeAreaInsets.left - view.safeAreaInsets.right, height: 60)
  150. dateLabel?.frame = CGRect(x: view.safeAreaInsets.left+20, y: 330, width: 80, height: 50)
  151. haveDateSwitch?.frame = CGRect(x: view.frame.size.width - 80 - view.safeAreaInsets.right, y: 330, width: 60, height: 50)
  152. timeLabel?.frame = CGRect(x: view.safeAreaInsets.left+20, y: 380, width: 100, height: 45)
  153. haveTimeSwitch?.frame = CGRect(x: view.frame.size.width - 80 - view.safeAreaInsets.right, y: 380, width: 60, height: 50)
  154. } else {
  155. doneBtn?.frame = CGRect(x: view.frame.size.width - 60, y: 5, width: 50, height: 50)
  156. colorView?.frame = CGRect(x: 0, y: 270, width: view.frame.size.width, height: 60)
  157. stampShapeViw?.frame = CGRect(x: 0, y: 180, width: view.frame.size.width, height: 90)
  158. dateLabel?.frame = CGRect(x: 20, y: 330, width: 80, height: 50)
  159. haveDateSwitch?.frame = CGRect(x: view.frame.size.width - 80, y: 330, width: 60, height: 50)
  160. timeLabel?.frame = CGRect(x: 20, y: 380, width: 100, height: 45)
  161. haveTimeSwitch?.frame = CGRect(x: view.frame.size.width - 80, y: 380, width: 60, height: 50)
  162. }
  163. }
  164. override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
  165. super.willTransition(to: newCollection, with: coordinator)
  166. updatePreferredContentSizeWithTraitCollection(traitCollection: newCollection)
  167. }
  168. // MARK: - Private Mthods
  169. func updatePreferredContentSizeWithTraitCollection(traitCollection: UITraitCollection) {
  170. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? 350 : 560)
  171. }
  172. // MARK: - Action
  173. @objc func buttonItemClicked_done(_ sender: Any) {
  174. let tStampItem = NSMutableDictionary()
  175. if stampTextField?.text != "" || (haveDateSwitch?.isOn ?? false) || (haveTimeSwitch?.isOn ?? false) {
  176. if (stampTextField?.text?.count ?? 0) > 0 {
  177. tStampItem["text"] = stampTextField?.text
  178. tStampItem["colorStyle"] = NSNumber(value:textStampColorStyle.rawValue)
  179. tStampItem["style"] = NSNumber(value:textStampStyle.rawValue)
  180. tStampItem["haveDate"] = NSNumber(booleanLiteral:haveDateSwitch?.isOn ?? false)
  181. tStampItem["haveTime"] = NSNumber(booleanLiteral:haveTimeSwitch?.isOn ?? false)
  182. } else {
  183. tStampItem["text"] = preView?.dateTime
  184. tStampItem["colorStyle"] = textStampColorStyle
  185. tStampItem["style"] = NSNumber(value:textStampStyle.rawValue)
  186. tStampItem["haveDate"] = NSNumber(booleanLiteral: false)
  187. tStampItem["haveTime"] = NSNumber(booleanLiteral: false)
  188. }
  189. delegate?.stampTextViewController?(self, dictionary: tStampItem)
  190. } else if (stampTextField?.text == "" && !(haveDateSwitch?.isOn ?? true) && !(haveTimeSwitch?.isOn ?? true)) {
  191. tStampItem["text"] = "StampText"
  192. tStampItem["colorStyle"] = NSNumber(value:textStampColorStyle.rawValue)
  193. tStampItem["style"] = NSNumber(value:textStampStyle.rawValue)
  194. tStampItem["haveDate"] = NSNumber(booleanLiteral: haveDateSwitch?.isOn ?? false)
  195. tStampItem["haveTime"] = NSNumber(booleanLiteral:haveTimeSwitch?.isOn ?? false)
  196. delegate?.stampTextViewController?(self, dictionary: tStampItem)
  197. }
  198. dismiss(animated: true, completion: nil)
  199. }
  200. @objc func buttonItemClicked_cancel(_ sender: Any) {
  201. dismiss(animated: true)
  202. }
  203. @objc func textFieldEditChange(_ sender: Any) {
  204. let textField = sender as? UITextField
  205. preView?.textStampText = textField?.text ?? ""
  206. preView?.setNeedsDisplay()
  207. }
  208. @objc func switchChange_date(_ sender: Any) {
  209. preView?.textStampHaveDate = haveDateSwitch?.isOn ?? false
  210. preView?.setNeedsDisplay()
  211. }
  212. @objc func switchChange_time(_ sender: Any) {
  213. preView?.textStampHaveTime = haveTimeSwitch?.isOn ?? false
  214. preView?.setNeedsDisplay()
  215. }
  216. // MARK: - UITextFieldDelegate
  217. func textFieldDidBeginEditing(_ textField: UITextField) {
  218. if UIApplication.shared.statusBarOrientation.isPortrait {
  219. preferredContentSize = CGSize(width: view.bounds.size.width, height: 800)
  220. } else {
  221. // Handle landscape orientation
  222. }
  223. }
  224. func textFieldDidEndEditing(_ textField: UITextField) {
  225. if UIApplication.shared.statusBarOrientation.isPortrait {
  226. preferredContentSize = CGSize(width: view.bounds.size.width, height: 560)
  227. } else {
  228. // Handle landscape orientation
  229. }
  230. }
  231. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  232. textField.resignFirstResponder()
  233. return true
  234. }
  235. // MARK: - CStampShapViewDelegate
  236. func stampShapView(_ stampShapView: CStampShapView, tag: Int) {
  237. switch TextStampType(rawValue: tag) {
  238. case .center:
  239. self.preView?.textStampStyle = .center
  240. self.textStampStyle = .center
  241. self.preView?.setNeedsDisplay()
  242. case .left:
  243. self.preView?.textStampStyle = .left
  244. self.textStampStyle = .left
  245. self.preView?.setNeedsDisplay()
  246. case .right:
  247. self.preView?.textStampStyle = .right
  248. self.textStampStyle = .right
  249. self.preView?.setNeedsDisplay()
  250. case .none?:
  251. self.preView?.textStampStyle = .none
  252. self.textStampStyle = .none
  253. self.preView?.setNeedsDisplay()
  254. default:
  255. break
  256. }
  257. }
  258. // MARK: - CPDFColorSelectViewDelegate
  259. func stampColorSelectView(_ stampColorSelectView: CStampColorSelectView, tag: Int) {
  260. switch tag {
  261. case 0:
  262. self.preView?.textStampColorStyle = .black
  263. self.textStampColorStyle = .black
  264. self.preView?.setNeedsDisplay()
  265. case 1:
  266. self.preView?.textStampColorStyle = .red
  267. self.textStampColorStyle = .red
  268. self.preView?.setNeedsDisplay()
  269. case 2:
  270. self.preView?.textStampColorStyle = .green
  271. self.textStampColorStyle = .green
  272. self.preView?.setNeedsDisplay()
  273. case 3:
  274. self.preView?.textStampColorStyle = .blue
  275. self.textStampColorStyle = .blue
  276. self.preView?.setNeedsDisplay()
  277. default:
  278. break
  279. }
  280. }
  281. }