InputBarButtonItem.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // InputBarButtonItem.swift
  3. // InputBarAccessoryView
  4. //
  5. // Copyright © 2017-2019 Nathan Tannar.
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in all
  15. // copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. // SOFTWARE.
  24. //
  25. // Created by Nathan Tannar on 8/18/17.
  26. //
  27. import UIKit
  28. /**
  29. A InputItem that inherits from UIButton
  30. ## Important Notes ##
  31. 1. Intended to be used in an `InputStackView`
  32. */
  33. open class InputBarButtonItem: UIButton, InputItem {
  34. /// The spacing properties of the InputBarButtonItem
  35. ///
  36. /// - fixed: The spacing is fixed
  37. /// - flexible: The spacing is flexible
  38. /// - none: There is no spacing
  39. public enum Spacing {
  40. case fixed(CGFloat)
  41. case flexible
  42. case none
  43. }
  44. public typealias InputBarButtonItemAction = ((InputBarButtonItem) -> Void)
  45. // MARK: - Properties
  46. /// A weak reference to the InputBarAccessoryView that the InputBarButtonItem used in
  47. open weak var inputBarAccessoryView: InputBarAccessoryView?
  48. /// The spacing property of the InputBarButtonItem that determines the contentHuggingPriority and any
  49. /// additional space to the intrinsicContentSize
  50. open var spacing: Spacing = .none {
  51. didSet {
  52. switch spacing {
  53. case .flexible:
  54. setContentHuggingPriority(UILayoutPriority(rawValue: 1), for: .horizontal)
  55. case .fixed:
  56. setContentHuggingPriority(UILayoutPriority(rawValue: 1000), for: .horizontal)
  57. case .none:
  58. setContentHuggingPriority(UILayoutPriority(rawValue: 500), for: .horizontal)
  59. }
  60. }
  61. }
  62. /// When not nil this size overrides the intrinsicContentSize
  63. private var size: CGSize? = CGSize(width: 20, height: 20) {
  64. didSet {
  65. invalidateIntrinsicContentSize()
  66. }
  67. }
  68. open override var intrinsicContentSize: CGSize {
  69. var contentSize = size ?? super.intrinsicContentSize
  70. switch spacing {
  71. case .fixed(let width):
  72. contentSize.width += width
  73. case .flexible, .none:
  74. break
  75. }
  76. return contentSize
  77. }
  78. /// A reference to the stack view position that the InputBarButtonItem is held in
  79. open var parentStackViewPosition: InputStackView.Position?
  80. /// The title for the UIControlState.normal
  81. open var title: String? {
  82. get {
  83. return title(for: .normal)
  84. }
  85. set {
  86. setTitle(newValue, for: .normal)
  87. }
  88. }
  89. /// The image for the UIControlState.normal
  90. open var image: UIImage? {
  91. get {
  92. return image(for: .normal)
  93. }
  94. set {
  95. setImage(newValue, for: .normal)
  96. }
  97. }
  98. /// Calls the onSelectedAction or onDeselectedAction when set
  99. open override var isHighlighted: Bool {
  100. get {
  101. return super.isHighlighted
  102. }
  103. set {
  104. guard newValue != isHighlighted else { return }
  105. super.isHighlighted = newValue
  106. if newValue {
  107. onSelectedAction?(self)
  108. } else {
  109. onDeselectedAction?(self)
  110. }
  111. }
  112. }
  113. /// Calls the onEnabledAction or onDisabledAction when set
  114. open override var isEnabled: Bool {
  115. didSet {
  116. if isEnabled {
  117. onEnabledAction?(self)
  118. } else {
  119. onDisabledAction?(self)
  120. }
  121. }
  122. }
  123. // MARK: - Reactive Hooks
  124. private var onTouchUpInsideAction: InputBarButtonItemAction?
  125. private var onKeyboardEditingBeginsAction: InputBarButtonItemAction?
  126. private var onKeyboardEditingEndsAction: InputBarButtonItemAction?
  127. private var onKeyboardSwipeGestureAction: ((InputBarButtonItem, UISwipeGestureRecognizer) -> Void)?
  128. private var onTextViewDidChangeAction: ((InputBarButtonItem, InputTextView) -> Void)?
  129. private var onSelectedAction: InputBarButtonItemAction?
  130. private var onDeselectedAction: InputBarButtonItemAction?
  131. private var onEnabledAction: InputBarButtonItemAction?
  132. private var onDisabledAction: InputBarButtonItemAction?
  133. // MARK: - Initialization
  134. public convenience init() {
  135. self.init(frame: .zero)
  136. }
  137. public override init(frame: CGRect) {
  138. super.init(frame: frame)
  139. setup()
  140. }
  141. required public init?(coder aDecoder: NSCoder) {
  142. super.init(coder: aDecoder)
  143. setup()
  144. }
  145. // MARK: - Setup
  146. /// Sets up the default properties
  147. open func setup() {
  148. contentVerticalAlignment = .center
  149. contentHorizontalAlignment = .center
  150. imageView?.contentMode = .scaleAspectFit
  151. setContentHuggingPriority(UILayoutPriority(rawValue: 500), for: .horizontal)
  152. setContentHuggingPriority(UILayoutPriority(rawValue: 500), for: .vertical)
  153. setTitleColor(UIColor(red: 0, green: 122/255, blue: 1, alpha: 1), for: .normal)
  154. setTitleColor(UIColor(red: 0, green: 122/255, blue: 1, alpha: 0.3), for: .highlighted)
  155. setTitleColor(.lightGray, for: .disabled)
  156. adjustsImageWhenHighlighted = false
  157. addTarget(self, action: #selector(InputBarButtonItem.touchUpInsideAction), for: .touchUpInside)
  158. }
  159. // MARK: - Size Adjustment
  160. /// Sets the size of the InputBarButtonItem which overrides the intrinsicContentSize. When set to nil
  161. /// the default intrinsicContentSize is used. The new size will be laid out in the UIStackView that
  162. /// the InputBarButtonItem is held in
  163. ///
  164. /// - Parameters:
  165. /// - newValue: The new size
  166. /// - animated: If the layout should be animated
  167. open func setSize(_ newValue: CGSize?, animated: Bool) {
  168. size = newValue
  169. if animated, let position = parentStackViewPosition {
  170. inputBarAccessoryView?.performLayout(animated) { [weak self] in
  171. self?.inputBarAccessoryView?.layoutStackViews([position])
  172. }
  173. }
  174. }
  175. // MARK: - Hook Setup Methods
  176. /// Used to setup your own initial properties
  177. ///
  178. /// - Parameter item: A reference to Self
  179. /// - Returns: Self
  180. @discardableResult
  181. open func configure(_ item: InputBarButtonItemAction) -> Self {
  182. item(self)
  183. return self
  184. }
  185. /// Sets the onKeyboardEditingBeginsAction
  186. ///
  187. /// - Parameter action: The new onKeyboardEditingBeginsAction
  188. /// - Returns: Self
  189. @discardableResult
  190. open func onKeyboardEditingBegins(_ action: @escaping InputBarButtonItemAction) -> Self {
  191. onKeyboardEditingBeginsAction = action
  192. return self
  193. }
  194. /// Sets the onKeyboardEditingEndsAction
  195. ///
  196. /// - Parameter action: The new onKeyboardEditingEndsAction
  197. /// - Returns: Self
  198. @discardableResult
  199. open func onKeyboardEditingEnds(_ action: @escaping InputBarButtonItemAction) -> Self {
  200. onKeyboardEditingEndsAction = action
  201. return self
  202. }
  203. /// Sets the onKeyboardSwipeGestureAction
  204. ///
  205. /// - Parameter action: The new onKeyboardSwipeGestureAction
  206. /// - Returns: Self
  207. @discardableResult
  208. open func onKeyboardSwipeGesture(_ action: @escaping (_ item: InputBarButtonItem, _ gesture: UISwipeGestureRecognizer) -> Void) -> Self {
  209. onKeyboardSwipeGestureAction = action
  210. return self
  211. }
  212. /// Sets the onTextViewDidChangeAction
  213. ///
  214. /// - Parameter action: The new onTextViewDidChangeAction
  215. /// - Returns: Self
  216. @discardableResult
  217. open func onTextViewDidChange(_ action: @escaping (_ item: InputBarButtonItem, _ textView: InputTextView) -> Void) -> Self {
  218. onTextViewDidChangeAction = action
  219. return self
  220. }
  221. /// Sets the onTouchUpInsideAction
  222. ///
  223. /// - Parameter action: The new onTouchUpInsideAction
  224. /// - Returns: Self
  225. @discardableResult
  226. open func onTouchUpInside(_ action: @escaping InputBarButtonItemAction) -> Self {
  227. onTouchUpInsideAction = action
  228. return self
  229. }
  230. /// Sets the onSelectedAction
  231. ///
  232. /// - Parameter action: The new onSelectedAction
  233. /// - Returns: Self
  234. @discardableResult
  235. open func onSelected(_ action: @escaping InputBarButtonItemAction) -> Self {
  236. onSelectedAction = action
  237. return self
  238. }
  239. /// Sets the onDeselectedAction
  240. ///
  241. /// - Parameter action: The new onDeselectedAction
  242. /// - Returns: Self
  243. @discardableResult
  244. open func onDeselected(_ action: @escaping InputBarButtonItemAction) -> Self {
  245. onDeselectedAction = action
  246. return self
  247. }
  248. /// Sets the onEnabledAction
  249. ///
  250. /// - Parameter action: The new onEnabledAction
  251. /// - Returns: Self
  252. @discardableResult
  253. open func onEnabled(_ action: @escaping InputBarButtonItemAction) -> Self {
  254. onEnabledAction = action
  255. return self
  256. }
  257. /// Sets the onDisabledAction
  258. ///
  259. /// - Parameter action: The new onDisabledAction
  260. /// - Returns: Self
  261. @discardableResult
  262. open func onDisabled(_ action: @escaping InputBarButtonItemAction) -> Self {
  263. onDisabledAction = action
  264. return self
  265. }
  266. // MARK: - InputItem Protocol
  267. /// Executes the onTextViewDidChangeAction with the given textView
  268. ///
  269. /// - Parameter textView: A reference to the InputTextView
  270. open func textViewDidChangeAction(with textView: InputTextView) {
  271. onTextViewDidChangeAction?(self, textView)
  272. }
  273. /// Executes the onKeyboardSwipeGestureAction with the given gesture
  274. ///
  275. /// - Parameter gesture: A reference to the gesture that was recognized
  276. open func keyboardSwipeGestureAction(with gesture: UISwipeGestureRecognizer) {
  277. onKeyboardSwipeGestureAction?(self, gesture)
  278. }
  279. /// Executes the onKeyboardEditingEndsAction
  280. open func keyboardEditingEndsAction() {
  281. onKeyboardEditingEndsAction?(self)
  282. }
  283. /// Executes the onKeyboardEditingBeginsAction
  284. open func keyboardEditingBeginsAction() {
  285. onKeyboardEditingBeginsAction?(self)
  286. }
  287. /// Executes the onTouchUpInsideAction
  288. @objc
  289. open func touchUpInsideAction() {
  290. onTouchUpInsideAction?(self)
  291. }
  292. // MARK: - Static Spacers
  293. /// An InputBarButtonItem that's spacing property is set to be .flexible
  294. public static var flexibleSpace: InputBarButtonItem {
  295. let item = InputBarButtonItem()
  296. item.setSize(.zero, animated: false)
  297. item.spacing = .flexible
  298. return item
  299. }
  300. /// An InputBarButtonItem that's spacing property is set to be .fixed with the width arguement
  301. public static func fixedSpace(_ width: CGFloat) -> InputBarButtonItem {
  302. let item = InputBarButtonItem()
  303. item.setSize(.zero, animated: false)
  304. item.spacing = .fixed(width)
  305. return item
  306. }
  307. }