CPDFDropDownMenu.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // CPDFDropDownMenu.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. protocol CPDFDropDownMenuDelegate: AnyObject {
  14. func dropDownMenu(_ menu: CPDFDropDownMenu, didEditWithText text: String)
  15. func dropDownMenu(_ menu: CPDFDropDownMenu, didSelectWithIndex index: Int)
  16. }
  17. class CPDFDropDownMenu: UIView, UITextFieldDelegate, UITableViewDelegate, UITableViewDataSource {
  18. weak var delegate: CPDFDropDownMenuDelegate?
  19. var editable:Bool = false
  20. var buttonImage:UIImage?
  21. var placeHolder:String?
  22. var textColor:UIColor?
  23. var font:UIFont?
  24. var pullDownButton:UIButton?
  25. var isShown:Bool = false
  26. var menuMaxHeight:CGFloat = 0
  27. var contextField:UITextField?
  28. private var privateOptionList: UITableView?
  29. var showBorder: Bool = false {
  30. didSet {
  31. if showBorder {
  32. layer.borderColor = UIColor.lightGray.cgColor
  33. layer.borderWidth = 0.5
  34. layer.masksToBounds = true
  35. layer.cornerRadius = 2.5
  36. } else {
  37. layer.borderColor = UIColor.clear.cgColor
  38. layer.masksToBounds = false
  39. layer.cornerRadius = 0
  40. layer.borderWidth = 0
  41. }
  42. }
  43. }
  44. var menuHeight: CGFloat = 0 {
  45. didSet {
  46. self.reloadData()
  47. }
  48. }
  49. var rowHeight: CGFloat = 0 {
  50. didSet {
  51. self.reloadData()
  52. }
  53. }
  54. var options: NSMutableArray = [] {
  55. didSet {
  56. self.reloadData()
  57. }
  58. }
  59. var defaultValue: String? {
  60. didSet {
  61. contextField?.text = defaultValue
  62. }
  63. }
  64. var optionList: UITableView {
  65. if let existingOptionList = privateOptionList {
  66. return existingOptionList
  67. } else {
  68. let frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y + self.frame.size.height, width: self.frame.size.width, height: 0)
  69. privateOptionList = UITableView(frame: frame, style: .plain)
  70. privateOptionList?.backgroundColor = UIColor(red: 0.9, green: 0.9, blue: 0.9, alpha: 1.0)
  71. privateOptionList?.delegate = self
  72. privateOptionList?.dataSource = self
  73. privateOptionList?.layer.borderColor = UIColor.lightGray.cgColor
  74. privateOptionList?.layer.borderWidth = 0.5
  75. privateOptionList?.allowsSelection = true
  76. if privateOptionList != nil {
  77. addSubview(privateOptionList!)
  78. }
  79. return privateOptionList!
  80. }
  81. }
  82. override init(frame: CGRect) {
  83. super.init(frame: frame)
  84. self.setUp()
  85. }
  86. required init?(coder: NSCoder) {
  87. fatalError("init(coder:) has not been implemented")
  88. }
  89. private func setUp() {
  90. contextField = UITextField(frame: CGRect.zero)
  91. contextField?.delegate = self
  92. contextField?.isEnabled = true
  93. contextField?.textColor = UIColor.darkGray
  94. if contextField != nil {
  95. addSubview(contextField!)
  96. }
  97. pullDownButton = UIButton(type: .custom)
  98. pullDownButton?.addTarget(self, action: #selector(showOrHide), for: .touchUpInside)
  99. pullDownButton?.setImage(UIImage(named: "CPDFEditArrow", in: Bundle(for: type(of: self)), compatibleWith: nil), for: .normal)
  100. if pullDownButton != nil {
  101. addSubview(pullDownButton!)
  102. }
  103. showBorder = true
  104. textColor = UIColor.darkGray
  105. font = UIFont.systemFont(ofSize: 16)
  106. rowHeight = 40
  107. isUserInteractionEnabled = true
  108. }
  109. @objc func showOrHide() {
  110. if isShown {
  111. UIView.animate(withDuration: 0.3, animations: {
  112. self.pullDownButton?.transform = CGAffineTransform(rotationAngle: .pi * 2)
  113. let frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y + self.frame.size.height - 0.5, width: self.frame.size.width, height: 0)
  114. let newFrame = self.convert(frame, to: self.superview?.superview)
  115. self.optionList.frame = newFrame
  116. }) { _ in
  117. self.pullDownButton?.transform = CGAffineTransform(rotationAngle: 0)
  118. self.isShown = false
  119. }
  120. } else {
  121. contextField?.resignFirstResponder()
  122. optionList.reloadData()
  123. UIView.animate(withDuration: 0.3, animations: {
  124. self.pullDownButton?.transform = CGAffineTransform(rotationAngle: .pi)
  125. let frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y + self.frame.size.height - 0.5, width: self.frame.size.width, height: self.menuHeight)
  126. let newFrame = self.convert(frame, to: self.superview?.superview)
  127. self.optionList.frame = newFrame
  128. }) { _ in
  129. self.isShown = true
  130. }
  131. }
  132. }
  133. func reloadData() {
  134. guard isShown else {
  135. return
  136. }
  137. optionList.reloadData()
  138. UIView.animate(withDuration: 0.3) {
  139. self.pullDownButton?.transform = CGAffineTransform(rotationAngle: .pi)
  140. let frame = CGRect(x: self.frame.origin.x, y: self.frame.origin.y + self.frame.size.height - 0.5, width: self.frame.size.width, height: self.menuHeight)
  141. let newFrame = self.convert(frame, to: self.superview?.superview)
  142. self.optionList.frame = newFrame
  143. }
  144. }
  145. override func layoutSubviews() {
  146. super.layoutSubviews()
  147. self.contextField?.frame = CGRect(x: 15, y: 5, width: self.frame.size.width - 50, height: self.frame.size.height - 10)
  148. self.pullDownButton?.frame = CGRect(x: self.frame.size.width - 35, y: 0, width: 30, height: 30)
  149. }
  150. // MARK: - UITextFieldDelegate
  151. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  152. if let text = textField.text, !text.isEmpty {
  153. defaultValue = text
  154. self.delegate?.dropDownMenu(self, didEditWithText: text)
  155. }
  156. return true
  157. }
  158. // MARK: - UITableViewDataSource
  159. func numberOfSections(in tableView: UITableView) -> Int {
  160. return 1
  161. }
  162. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  163. return self.options.count
  164. }
  165. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  166. let reuseIdentifier = "tableViewIdentifier"
  167. var cell = tableView.dequeueReusableCell(withIdentifier: reuseIdentifier)
  168. if cell == nil {
  169. cell = UITableViewCell(style: .default, reuseIdentifier: reuseIdentifier)
  170. }
  171. cell?.textLabel?.text = self.options[indexPath.row] as? String
  172. cell?.textLabel?.font = self.font
  173. cell?.textLabel?.textColor = self.textColor
  174. cell?.isUserInteractionEnabled = true
  175. return cell!
  176. }
  177. // MARK: - UITableViewDelegate
  178. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  179. return self.rowHeight
  180. }
  181. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  182. contextField!.text = self.options[indexPath.row] as? String
  183. defaultValue = contextField!.text
  184. self.delegate?.dropDownMenu(self, didSelectWithIndex: indexPath.row)
  185. showOrHide()
  186. }
  187. }