CPDFStampViewController.swift 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. //
  2. // CPDFStampViewController.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 UIKit
  14. @objc protocol CPDFStampViewControllerDelegate: AnyObject {
  15. @objc optional func stampViewController(_ stampViewController: CPDFStampViewController, selectedIndex: Int, stamp: [String: Any])
  16. @objc optional func stampViewControllerDismiss(_ stampViewController: CPDFStampViewController)
  17. }
  18. typealias PDFAnnotationStampKey = String
  19. let PDFAnnotationStampKeyType: PDFAnnotationStampKey = "PDFAnnotationStampKeyType"
  20. let PDFAnnotationStampKeyImagePath: PDFAnnotationStampKey = "PDFAnnotationStampKeyImagePath"
  21. let PDFAnnotationStampKeyText: PDFAnnotationStampKey = "PDFAnnotationStampKeyText"
  22. let PDFAnnotationStampKeyShowDate: PDFAnnotationStampKey = "PDFAnnotationStampKeyShowDate"
  23. let PDFAnnotationStampKeyShowTime: PDFAnnotationStampKey = "PDFAnnotationStampKeyShowTime"
  24. let PDFAnnotationStampKeyStyle: PDFAnnotationStampKey = "PDFAnnotationStampKeyStyle"
  25. let PDFAnnotationStampKeyShape: PDFAnnotationStampKey = "PDFAnnotationStampKeyShape"
  26. let kStamp_Cell_Height = 60
  27. class CPDFStampViewController: UIViewController,UICollectionViewDelegate, UICollectionViewDataSource, UIPopoverPresentationControllerDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITableViewDelegate, UITableViewDataSource, CStampTextViewControllerDelegate, CCustomizeStampTableViewCellDelegate {
  28. weak var delegate: CPDFStampViewControllerDelegate?
  29. private var collectView: UICollectionView?
  30. private var tableView: UITableView?
  31. private var segmentedControl: UISegmentedControl?
  32. private var standardArray: [Any]?
  33. private var customTextArray: [Any]?
  34. private var customImageArray: [Any]?
  35. private var imgDicCache: NSMutableDictionary?
  36. private var backBtn: UIButton?
  37. private var titleLabel: UILabel?
  38. private var createButton: UIButton?
  39. private var emptyLabel: UILabel?
  40. private var standardView: UIView?
  41. private var customizeView: UIView?
  42. private var stampFileManager: CStampFileManager?
  43. private var textButton: CStampButton?
  44. private var imageButton: CStampButton?
  45. private var modelView: UIView?
  46. private var headerView: UIView?
  47. // MARK: - ViewController Methods
  48. override func viewDidLoad() {
  49. super.viewDidLoad()
  50. // Do any additional setup after loading the view.
  51. view.backgroundColor = CPDFColorUtils.CAnnotationSampleBackgoundColor()
  52. headerView = UIView()
  53. headerView?.layer.borderColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.1).cgColor
  54. headerView?.layer.borderWidth = 1.0
  55. headerView?.backgroundColor = CPDFColorUtils.CAnnotationPropertyViewControllerBackgoundColor()
  56. if headerView != nil {
  57. view.addSubview(headerView!)
  58. }
  59. titleLabel = UILabel()
  60. titleLabel?.autoresizingMask = .flexibleRightMargin
  61. titleLabel?.text = NSLocalizedString("Stamp", comment: "")
  62. titleLabel?.textAlignment = .center
  63. titleLabel?.font = UIFont.systemFont(ofSize: 20)
  64. titleLabel?.adjustsFontSizeToFitWidth = true
  65. if titleLabel != nil {
  66. headerView?.addSubview(titleLabel!)
  67. }
  68. backBtn = UIButton()
  69. backBtn?.autoresizingMask = .flexibleLeftMargin
  70. backBtn?.setImage(UIImage(named: "CPDFAnnotationBaseImageBack", in: Bundle(for: Self.self), compatibleWith: nil), for: .normal)
  71. backBtn?.addTarget(self, action: #selector(buttonItemClicked_back(_:)), for: .touchUpInside)
  72. if backBtn != nil {
  73. headerView?.addSubview(backBtn!)
  74. }
  75. let segmmentArray = [NSLocalizedString("Standard", comment: ""), NSLocalizedString("Custom", comment: "")]
  76. segmentedControl = UISegmentedControl(items: segmmentArray)
  77. segmentedControl?.selectedSegmentIndex = 0
  78. segmentedControl?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  79. segmentedControl?.addTarget(self, action: #selector(segmentedControlValueChanged_singature(_:)), for: .valueChanged)
  80. if segmentedControl != nil {
  81. view.addSubview(segmentedControl!)
  82. }
  83. stampFileManager = CStampFileManager()
  84. stampFileManager?.readStampDataFromFile()
  85. customTextArray = stampFileManager?.getTextStampData()
  86. customImageArray = stampFileManager?.getImageStampData()
  87. // StandardView
  88. createStandardView()
  89. // CustomizeView
  90. createCustomizeView()
  91. // Data
  92. var array = [String]()
  93. for i in 1..<13 {
  94. var tPicName: String?
  95. if i < 10 {
  96. tPicName = "CPDFStampImage-0\(i).png"
  97. } else {
  98. tPicName = "CPDFStampImage-\(i).png"
  99. }
  100. array.append(tPicName!)
  101. }
  102. array.append(contentsOf: ["CPDFStampImage-13", "CPDFStampImage-14", "CPDFStampImage-15", "CPDFStampImage-16", "CPDFStampImage-20", "CPDFStampImage-18", "CPDFStampImage_chick", "CPDFStampImage_cross", "CPDFStampImage_circle"])
  103. standardArray = array
  104. imgDicCache = NSMutableDictionary()
  105. createGestureRecognizer()
  106. updatePreferredContentSizeWithTraitCollection(traitCollection: traitCollection)
  107. }
  108. override func viewWillLayoutSubviews() {
  109. super.viewWillLayoutSubviews()
  110. titleLabel?.frame = CGRect(x: (view.frame.size.width - 120)/2, y: 0, width: 120, height: 50)
  111. segmentedControl?.frame = CGRect(x: 50, y: 55, width: view.frame.size.width-100, height: 30)
  112. headerView?.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height: 50)
  113. emptyLabel?.frame = CGRect(x: (view.frame.size.width - 120)/2, y: (view.frame.size.height - 50)/2, width: 120, height: 50)
  114. if #available(iOS 11.0, *) {
  115. backBtn?.frame = CGRect(x: view.frame.size.width - 60 - view.safeAreaInsets.right, y: 5, width: 50, height: 50)
  116. createButton?.frame = CGRect(x: view.frame.size.width - 70 - view.safeAreaInsets.right, y: view.bounds.size.height - 200 - view.safeAreaInsets.bottom, width: 50, height: 50)
  117. textButton?.frame = CGRect(x: view.frame.size.width - 180 - view.safeAreaInsets.right, y: view.bounds.size.height - 320 - view.safeAreaInsets.bottom, width: 160, height: 40)
  118. imageButton?.frame = CGRect(x: view.frame.size.width - 180 - view.safeAreaInsets.right, y: view.bounds.size.height - 270 - view.safeAreaInsets.bottom, width: 160, height: 40)
  119. } else {
  120. backBtn?.frame = CGRect(x: view.frame.size.width - 60, y: 5, width: 50, height: 50)
  121. createButton?.frame = CGRect(x: view.frame.size.width - 60, y: view.frame.size.height - 200, width: 50, height: 50)
  122. textButton?.frame = CGRect(x: view.frame.size.width - 180, y: view.frame.size.height - 320, width: 160, height: 40)
  123. imageButton?.frame = CGRect(x: view.frame.size.width - 180, y: view.frame.size.height - 270, width: 160, height: 40)
  124. }
  125. modelView?.frame = CGRect(x: 0, y: -200, width: view.bounds.size.width, height: view.bounds.size.height+200)
  126. }
  127. override func viewWillAppear(_ animated: Bool) {
  128. super.viewWillAppear(animated)
  129. collectView?.reloadData()
  130. }
  131. override func willTransition(to newCollection: UITraitCollection, with coordinator: UIViewControllerTransitionCoordinator) {
  132. super.willTransition(to: newCollection, with: coordinator)
  133. updatePreferredContentSizeWithTraitCollection(traitCollection: newCollection)
  134. }
  135. // MARK: - Protect Methods
  136. func updatePreferredContentSizeWithTraitCollection(traitCollection: UITraitCollection) {
  137. let width = UIScreen.main.bounds.size.width
  138. let height = UIScreen.main.bounds.size.height
  139. let mWidth = min(width, height)
  140. let mHeight = max(width, height)
  141. let currentDevice = UIDevice.current
  142. if currentDevice.userInterfaceIdiom == .pad {
  143. // This is an iPad
  144. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? mWidth * 0.5 : mHeight * 0.6)
  145. } else {
  146. // This is an iPhone or iPod touch
  147. self.preferredContentSize = CGSize(width: self.view.bounds.size.width, height: traitCollection.verticalSizeClass == .compact ? mWidth * 0.9 : mHeight * 0.9)
  148. }
  149. }
  150. // MARK: - Private Methods
  151. func createStandardView() {
  152. standardView = UIView(frame: CGRect(x: 0, y: 100, width: view.bounds.size.width, height: view.bounds.size.height-100))
  153. standardView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  154. if standardView != nil {
  155. view.addSubview(standardView!)
  156. }
  157. let layout = UICollectionViewFlowLayout()
  158. layout.scrollDirection = .vertical
  159. layout.minimumInteritemSpacing = 10
  160. layout.minimumLineSpacing = 10
  161. layout.itemSize = CGSize(width: 170, height: 80)
  162. layout.sectionInset = UIEdgeInsets(top: 5, left: 20, bottom: 5, right: 20)
  163. collectView = UICollectionView(frame: standardView?.bounds ?? .zero, collectionViewLayout: layout)
  164. collectView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  165. collectView?.delegate = self
  166. collectView?.dataSource = self
  167. collectView?.backgroundColor = UIColor.clear
  168. collectView?.register(StampCollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header")
  169. collectView?.register(StampCollectionHeaderView1.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "header1")
  170. collectView?.register(CStampCollectionViewCell.self, forCellWithReuseIdentifier: "TStampViewCell")
  171. if #available(iOS 11.0, *) {
  172. collectView?.contentInsetAdjustmentBehavior = .always
  173. }
  174. if collectView != nil {
  175. standardView?.addSubview(collectView!)
  176. }
  177. }
  178. func createCustomizeView() {
  179. customizeView = UIView(frame: CGRect(x: 0, y: 100, width: view.bounds.size.width, height: view.bounds.size.height-100))
  180. customizeView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  181. if customizeView != nil {
  182. view.addSubview(customizeView!)
  183. }
  184. customizeView?.isHidden = true
  185. tableView = UITableView(frame: customizeView?.bounds ?? .zero, style: .grouped)
  186. tableView?.delegate = self
  187. tableView?.dataSource = self
  188. tableView?.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  189. tableView?.backgroundColor = .clear
  190. tableView?.rowHeight = 60
  191. if tableView != nil {
  192. customizeView?.addSubview(tableView!)
  193. }
  194. emptyLabel = UILabel()
  195. emptyLabel?.text = NSLocalizedString("NO Custom", comment: "")
  196. emptyLabel?.textAlignment = .center
  197. if emptyLabel != nil {
  198. customizeView?.addSubview(emptyLabel!)
  199. }
  200. if (customImageArray?.count ?? 0) < 1 && (customTextArray?.count ?? 0) < 1 {
  201. tableView?.isHidden = true
  202. emptyLabel?.isHidden = false
  203. } else {
  204. emptyLabel?.isHidden = true
  205. tableView?.isHidden = false
  206. }
  207. modelView = UIView()
  208. modelView?.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
  209. if modelView != nil {
  210. customizeView?.addSubview(modelView!)
  211. }
  212. modelView?.isHidden = true
  213. createButton = UIButton()
  214. createButton?.layer.cornerRadius = 25.0
  215. createButton?.clipsToBounds = true
  216. createButton?.setImage(UIImage(named: "CPDFSignatureImageAdd", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  217. createButton?.backgroundColor = .blue
  218. createButton?.addTarget(self, action: #selector(buttonItemClicked_create(_:)), for: .touchUpInside)
  219. if createButton != nil {
  220. customizeView?.addSubview(createButton!)
  221. }
  222. textButton = CStampButton()
  223. textButton?.stampBtn?.setImage(UIImage(named: "CPDFStampImageText", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  224. textButton?.titleLabel?.text = NSLocalizedString("Text Stamp", comment: "")
  225. textButton?.stampBtn?.addTarget(self, action: #selector(buttonItemClicked_text(_:)), for: .touchUpInside)
  226. if textButton != nil {
  227. customizeView?.addSubview(textButton!)
  228. }
  229. textButton?.isHidden = true
  230. imageButton = CStampButton()
  231. imageButton?.stampBtn?.setImage(UIImage(named: "CPDFStampImageImage", in: Bundle(for: self.classForCoder), compatibleWith: nil), for: .normal)
  232. imageButton?.titleLabel?.text = NSLocalizedString("Image Stamp", comment: "")
  233. imageButton?.stampBtn?.addTarget(self, action: #selector(buttonItemClicked_image(_:)), for: .touchUpInside)
  234. if imageButton != nil {
  235. customizeView?.addSubview(imageButton!)
  236. }
  237. imageButton?.isHidden = true
  238. }
  239. func createGestureRecognizer() {
  240. createButton?.isUserInteractionEnabled = true
  241. modelView?.isUserInteractionEnabled = true
  242. let panRecognizer = UIPanGestureRecognizer(target: self, action: #selector(panaddBookmarkBtn(_:)))
  243. createButton?.addGestureRecognizer(panRecognizer)
  244. let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(tapModelView(_:)))
  245. modelView?.addGestureRecognizer(tapRecognizer)
  246. }
  247. @objc func panaddBookmarkBtn(_ gestureRecognizer: UIPanGestureRecognizer) {
  248. let point = gestureRecognizer.translation(in: view)
  249. let newX = (createButton?.center.x ?? 0) + point.x
  250. let newY = (createButton?.center.y ?? 0) + point.y
  251. if view.frame.contains(CGPoint(x: newX, y: newY)) {
  252. createButton?.center = CGPoint(x: newX, y: newY)
  253. }
  254. gestureRecognizer.setTranslation(CGPoint.zero, in: view)
  255. }
  256. @objc func tapModelView(_ gestureRecognizer: UITapGestureRecognizer) {
  257. textButton?.isHidden = true
  258. modelView?.isHidden = true
  259. imageButton?.isHidden = true
  260. }
  261. func createImageSignature() {
  262. let cameraAction = UIAlertAction(title: NSLocalizedString("Camera", comment: ""), style: .default) { (action) in
  263. let imagePickerController = UIImagePickerController()
  264. imagePickerController.delegate = self
  265. imagePickerController.sourceType = .camera
  266. self.present(imagePickerController, animated: true, completion: nil)
  267. }
  268. let photoAction = UIAlertAction(title: NSLocalizedString("Choose from Album", comment: ""), style: .default) { (action) in
  269. let imagePickerController = UIImagePickerController()
  270. imagePickerController.delegate = self
  271. imagePickerController.sourceType = .photoLibrary
  272. imagePickerController.allowsEditing = true
  273. imagePickerController.modalPresentationStyle = .popover
  274. if UIDevice.current.userInterfaceIdiom == .pad {
  275. imagePickerController.popoverPresentationController?.sourceView = self.imageButton
  276. imagePickerController.popoverPresentationController?.sourceRect = self.imageButton?.bounds ?? .zero
  277. }
  278. self.present(imagePickerController, animated: true, completion: nil)
  279. }
  280. let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)
  281. let actionSheet = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  282. if UIDevice.current.userInterfaceIdiom == .pad {
  283. actionSheet.popoverPresentationController?.sourceView = self.imageButton
  284. actionSheet.popoverPresentationController?.sourceRect = self.imageButton?.bounds ?? .zero
  285. }
  286. actionSheet.addAction(cameraAction)
  287. actionSheet.addAction(photoAction)
  288. actionSheet.addAction(cancelAction)
  289. actionSheet.modalPresentationStyle = .popover
  290. self.present(actionSheet, animated: true, completion: nil)
  291. }
  292. func compressImage(_ image: UIImage) -> UIImage {
  293. var maxWH = CGFloat(kStamp_Cell_Height)
  294. if UIScreen.main.responds(to: #selector(getter: UIScreen.scale)) {
  295. maxWH *= UIScreen.main.scale
  296. }
  297. var imageScale: CGFloat = 1.0
  298. if image.size.width > maxWH || image.size.height > maxWH {
  299. imageScale = min(maxWH / image.size.width, maxWH / image.size.height)
  300. }
  301. let newSize = CGSize(width: image.size.width * imageScale, height: image.size.height * imageScale)
  302. UIGraphicsBeginImageContext(newSize)
  303. image.draw(in: CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height))
  304. let newImage = UIGraphicsGetImageFromCurrentImageContext()
  305. UIGraphicsEndImageContext()
  306. return newImage ?? UIImage()
  307. }
  308. // MARK: - UIImagePickerControllerDelegate
  309. func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  310. picker.dismiss(animated: true, completion: nil)
  311. var image: UIImage?
  312. if let editedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
  313. image = editedImage
  314. } else if let originalImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
  315. image = originalImage
  316. }
  317. if var image = image {
  318. let imageOrientation = image.imageOrientation
  319. if imageOrientation != .up {
  320. UIGraphicsBeginImageContext(image.size)
  321. image.draw(in: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
  322. image = UIGraphicsGetImageFromCurrentImageContext()!
  323. UIGraphicsEndImageContext()
  324. }
  325. if let imageData = image.pngData(), imageData.count > 0 {
  326. image = UIImage(data: imageData)!
  327. }
  328. if(image.cgImage != nil) {
  329. let colorMasking: [CGFloat] = [222, 255, 222, 255, 222, 255]
  330. if let imageRef = image.cgImage!.copy(maskingColorComponents: colorMasking){
  331. image = UIImage(cgImage: imageRef)
  332. }
  333. if let tPath = self.stampFileManager?.saveStamp(with: image) {
  334. var tStampItem = [String: Any]()
  335. tStampItem["path"] = tPath
  336. self.stampFileManager?.insertStampItem(tStampItem as NSDictionary, type: .image)
  337. customImageArray = self.stampFileManager?.getImageStampData()
  338. self.tableView?.reloadData()
  339. if (customImageArray?.count ?? 0) < 1 && (customTextArray?.count ?? 0) < 1 {
  340. emptyLabel?.isHidden = false
  341. tableView?.isHidden = true
  342. } else {
  343. emptyLabel?.isHidden = true
  344. tableView?.isHidden = false
  345. }
  346. }
  347. }
  348. }
  349. }
  350. func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
  351. picker.dismiss(animated: true)
  352. }
  353. // MARK: - Action
  354. @objc func buttonItemClicked_back(_ sender: Any) {
  355. dismiss(animated: true, completion: nil)
  356. delegate?.stampViewControllerDismiss?(self)
  357. }
  358. @objc func buttonItemClicked_create(_ sender: Any) {
  359. textButton?.isHidden = !(textButton?.isHidden ?? false)
  360. modelView?.isHidden = !(modelView?.isHidden ?? false)
  361. imageButton?.isHidden = !(imageButton?.isHidden ?? false)
  362. }
  363. @objc func segmentedControlValueChanged_singature(_ sender: Any) {
  364. if segmentedControl?.selectedSegmentIndex == 0 {
  365. standardView?.isHidden = false
  366. customizeView?.isHidden = true
  367. } else {
  368. standardView?.isHidden = true
  369. customizeView?.isHidden = false
  370. }
  371. }
  372. @objc func buttonItemClicked_text(_ sender: Any) {
  373. textButton?.isHidden = true
  374. modelView?.isHidden = true
  375. imageButton?.isHidden = true
  376. let stampTextVC = CStampTextViewController()
  377. let presentationController = AAPLCustomPresentationController(presentedViewController: stampTextVC, presenting: self)
  378. stampTextVC.delegate = self
  379. stampTextVC.transitioningDelegate = presentationController
  380. self.present(stampTextVC, animated: true)
  381. }
  382. @objc func buttonItemClicked_image(_ sender: Any) {
  383. textButton?.isHidden = true
  384. modelView?.isHidden = true
  385. imageButton?.isHidden = true
  386. createImageSignature()
  387. }
  388. // MARK: - UICollectionViewDataSource
  389. func numberOfSections(in collectionView: UICollectionView) -> Int {
  390. return 1
  391. }
  392. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
  393. return standardArray?.count ?? 0
  394. }
  395. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  396. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TStampViewCell", for: indexPath) as? CStampCollectionViewCell
  397. cell?.editing = false
  398. cell?.stampImage?.image = UIImage(named: standardArray?[indexPath.item] as? String ?? "", in: Bundle(for: self.classForCoder), compatibleWith: nil)
  399. if(cell != nil) {
  400. return cell!
  401. } else {
  402. return UICollectionViewCell.init()
  403. }
  404. }
  405. // MARK: - UICollectionViewDelegate
  406. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  407. dismiss(animated: true)
  408. delegate?.stampViewController?(self, selectedIndex: indexPath.row, stamp: [String : Any]())
  409. }
  410. // MARK: - UITableViewDataSource
  411. func numberOfSections(in tableView: UITableView) -> Int {
  412. return 2
  413. }
  414. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  415. switch section {
  416. case 0:
  417. return customTextArray?.count ?? 0
  418. case 1:
  419. return customImageArray?.count ?? 0
  420. default:
  421. return 0
  422. }
  423. }
  424. func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  425. switch section {
  426. case 0:
  427. return NSLocalizedString("Text Stamp", comment: "")
  428. case 1:
  429. return NSLocalizedString("Image Stamp", comment: "")
  430. default:
  431. return ""
  432. }
  433. }
  434. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  435. var cell = tableView.dequeueReusableCell(withIdentifier: "cell") as? CCustomizeStampTableViewCell
  436. if (cell == nil) {
  437. cell = CCustomizeStampTableViewCell(style: .subtitle, reuseIdentifier: "cell")
  438. }
  439. if (self.customTextArray?.count ?? 0) > 0 || (self.customImageArray?.count ?? 0) > 0 {
  440. if indexPath.section == 0 {
  441. let tDic = self.customTextArray?[indexPath.item]
  442. let tText = (tDic as? NSDictionary)?["text"] as? String ?? ""
  443. let tStyle = (tDic as? NSDictionary)?["style"] as? Int ?? 0
  444. let tColorStyle = (tDic as? NSDictionary)?["colorStyle"] as? Int ?? 0
  445. let tHaveDate = (tDic as? NSDictionary)?["haveDate"] as? Bool ?? false
  446. let tHaveTime = (tDic as? NSDictionary)?["haveTime"] as? Bool ?? false
  447. let tPreview = CStampPreview(frame: CGRect(x: 0, y: 0, width: 320, height: kStamp_Cell_Height))
  448. tPreview.textStampText = tText
  449. tPreview.textStampColorStyle = TextStampColorType(rawValue: tColorStyle)!
  450. tPreview.textStampStyle = TextStampType(rawValue: tStyle)!
  451. tPreview.textStampHaveDate = tHaveDate
  452. tPreview.textStampHaveTime = tHaveTime
  453. tPreview.leftMargin = 0
  454. let tImg = tPreview.renderImage()
  455. cell!.customizeStampImageView?.image = tImg
  456. } else {
  457. let tDic = self.customImageArray?[indexPath.item]
  458. if let img = self.imgDicCache?.object(forKey: tDic) {
  459. cell!.customizeStampImageView?.image = img as? UIImage
  460. } else {
  461. if let tPath = (tDic as? NSDictionary)?["path"] as? String {
  462. let tFileName = FileManager.default.displayName(atPath: tPath)
  463. let tRealPath = "\(kPDFStampDataFolder)/\(tFileName)"
  464. if let tImg = UIImage(contentsOfFile: tRealPath) {
  465. let img = compressImage(tImg)
  466. self.imgDicCache?.setObject(img, forKey: tDic as! NSCopying)
  467. cell!.customizeStampImageView?.image = img
  468. }
  469. }
  470. }
  471. }
  472. }
  473. cell!.deleteDelegate = self
  474. return cell!
  475. }
  476. // MARK: - UITableViewDelegate
  477. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  478. if indexPath.section == 0 {
  479. let tDic = self.customTextArray?[indexPath.item]
  480. let tText = (tDic as? NSDictionary)?["text"] as? String ?? ""
  481. let tStyle = (tDic as? NSDictionary)?["style"] as? Int ?? 0
  482. let tColorStyle = (tDic as? NSDictionary)?["colorStyle"] as? Int ?? 0
  483. let tHaveDate = (tDic as? NSDictionary)?["haveDate"] as? Bool ?? false
  484. let tHaveTime = (tDic as? NSDictionary)?["haveTime"] as? Bool ?? false
  485. var stampStype: Int = 0
  486. var stampShape: Int = 0
  487. switch TextStampColorType(rawValue: tColorStyle)! {
  488. case .black:
  489. stampStype = 0
  490. case .red:
  491. stampStype = 1
  492. case .green:
  493. stampStype = 2
  494. case .blue:
  495. stampStype = 3
  496. }
  497. switch TextStampType(rawValue: tStyle)! {
  498. case .none:
  499. stampShape = 3
  500. case .right:
  501. stampShape = 2
  502. case .left:
  503. stampShape = 1
  504. case .center:
  505. stampShape = 0
  506. }
  507. dismiss(animated: true)
  508. delegate?.stampViewController?(self, selectedIndex: indexPath.row, stamp: [PDFAnnotationStampKeyText: tText,
  509. PDFAnnotationStampKeyShowDate: tHaveDate,
  510. PDFAnnotationStampKeyShowTime: tHaveTime,
  511. PDFAnnotationStampKeyStyle: stampStype,
  512. PDFAnnotationStampKeyShape: stampShape])
  513. } else if indexPath.section == 1 {
  514. guard let tDict = self.customImageArray?[indexPath.row] as? [String: Any],
  515. let tPath = tDict["path"] as? String else {
  516. return
  517. }
  518. let tFileName = FileManager.default.displayName(atPath: tPath)
  519. let tRealPath = "\(kPDFStampDataFolder)/\(tFileName)"
  520. dismiss(animated: true)
  521. delegate?.stampViewController?(self, selectedIndex: indexPath.row, stamp: [PDFAnnotationStampKeyImagePath: tRealPath])
  522. }
  523. }
  524. // MARK: - CCustomizeStampTableViewCellDelegate
  525. func customizeStampTableViewCell(_ customizeStampTableViewCell: CCustomizeStampTableViewCell) {
  526. let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel, handler: nil)
  527. let OKAction = UIAlertAction(title: NSLocalizedString("OK", comment: ""), style: .default) { (action) in
  528. if let select = self.tableView?.indexPath(for: customizeStampTableViewCell) {
  529. if select.section == 0 {
  530. self.stampFileManager?.removeStampItem(index: select.row, stampType: .text)
  531. self.customTextArray = self.stampFileManager?.getTextStampData()
  532. } else if select.section == 1 {
  533. self.stampFileManager?.removeStampItem(index: select.row, stampType: .image)
  534. self.customImageArray = self.stampFileManager?.getImageStampData()
  535. }
  536. self.tableView?.reloadData()
  537. if (self.customImageArray?.count ?? 0) < 1 && (self.customTextArray?.count ?? 0) < 1 {
  538. self.emptyLabel?.isHidden = false
  539. self.tableView?.isHidden = true
  540. } else {
  541. self.emptyLabel?.isHidden = true
  542. self.tableView?.isHidden = false
  543. }
  544. }
  545. }
  546. let alert = UIAlertController(title: NSLocalizedString("Warning", comment: ""), message: NSLocalizedString("Are you sure to delete?", comment: ""), preferredStyle: .alert)
  547. alert.addAction(cancelAction)
  548. alert.addAction(OKAction)
  549. present(alert, animated: true, completion: nil)
  550. }
  551. // MARK: - CStampTextViewControllerDelegate
  552. func stampTextViewController(_ stampTextViewController: CStampTextViewController, dictionary: NSDictionary) {
  553. self.stampFileManager?.insertStampItem(dictionary, type: .text )
  554. self.customTextArray = self.stampFileManager?.getTextStampData()
  555. self.tableView?.reloadData()
  556. if (self.customImageArray?.count ?? 0) < 1 && (self.customTextArray?.count ?? 0) < 1 {
  557. self.emptyLabel?.isHidden = false
  558. self.tableView?.isHidden = true
  559. } else {
  560. self.emptyLabel?.isHidden = true
  561. self.tableView?.isHidden = false
  562. }
  563. }
  564. }