InputBarButtonItem.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. //
  2. // InputBarButtonItem.swift
  3. // InputBarAccessoryView
  4. //
  5. // Copyright © 2017-2020 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(.systemBlue, for: .normal)
  154. setTitleColor(UIColor.systemBlue.withAlphaComponent(0.3), for: .highlighted)
  155. if #available(iOS 13, *) {
  156. setTitleColor(.systemGray2, for: .disabled)
  157. } else {
  158. setTitleColor(.lightGray, for: .disabled)
  159. }
  160. adjustsImageWhenHighlighted = false
  161. addTarget(self, action: #selector(InputBarButtonItem.touchUpInsideAction), for: .touchUpInside)
  162. }
  163. // MARK: - Size Adjustment
  164. /// Sets the size of the InputBarButtonItem which overrides the intrinsicContentSize. When set to nil
  165. /// the default intrinsicContentSize is used. The new size will be laid out in the UIStackView that
  166. /// the InputBarButtonItem is held in
  167. ///
  168. /// - Parameters:
  169. /// - newValue: The new size
  170. /// - animated: If the layout should be animated
  171. open func setSize(_ newValue: CGSize?, animated: Bool) {
  172. size = newValue
  173. if animated, let position = parentStackViewPosition {
  174. inputBarAccessoryView?.performLayout(animated) { [weak self] in
  175. self?.inputBarAccessoryView?.layoutStackViews([position])
  176. }
  177. }
  178. }
  179. // MARK: - Hook Setup Methods
  180. /// Used to setup your own initial properties
  181. ///
  182. /// - Parameter item: A reference to Self
  183. /// - Returns: Self
  184. @discardableResult
  185. open func configure(_ item: InputBarButtonItemAction) -> Self {
  186. item(self)
  187. return self
  188. }
  189. /// Sets the onKeyboardEditingBeginsAction
  190. ///
  191. /// - Parameter action: The new onKeyboardEditingBeginsAction
  192. /// - Returns: Self
  193. @discardableResult
  194. open func onKeyboardEditingBegins(_ action: @escaping InputBarButtonItemAction) -> Self {
  195. onKeyboardEditingBeginsAction = action
  196. return self
  197. }
  198. /// Sets the onKeyboardEditingEndsAction
  199. ///
  200. /// - Parameter action: The new onKeyboardEditingEndsAction
  201. /// - Returns: Self
  202. @discardableResult
  203. open func onKeyboardEditingEnds(_ action: @escaping InputBarButtonItemAction) -> Self {
  204. onKeyboardEditingEndsAction = action
  205. return self
  206. }
  207. /// Sets the onKeyboardSwipeGestureAction
  208. ///
  209. /// - Parameter action: The new onKeyboardSwipeGestureAction
  210. /// - Returns: Self
  211. @discardableResult
  212. open func onKeyboardSwipeGesture(_ action: @escaping (_ item: InputBarButtonItem, _ gesture: UISwipeGestureRecognizer) -> Void) -> Self {
  213. onKeyboardSwipeGestureAction = action
  214. return self
  215. }
  216. /// Sets the onTextViewDidChangeAction
  217. ///
  218. /// - Parameter action: The new onTextViewDidChangeAction
  219. /// - Returns: Self
  220. @discardableResult
  221. open func onTextViewDidChange(_ action: @escaping (_ item: InputBarButtonItem, _ textView: InputTextView) -> Void) -> Self {
  222. onTextViewDidChangeAction = action
  223. return self
  224. }
  225. /// Sets the onTouchUpInsideAction
  226. ///
  227. /// - Parameter action: The new onTouchUpInsideAction
  228. /// - Returns: Self
  229. @discardableResult
  230. open func onTouchUpInside(_ action: @escaping InputBarButtonItemAction) -> Self {
  231. onTouchUpInsideAction = action
  232. return self
  233. }
  234. /// Sets the onSelectedAction
  235. ///
  236. /// - Parameter action: The new onSelectedAction
  237. /// - Returns: Self
  238. @discardableResult
  239. open func onSelected(_ action: @escaping InputBarButtonItemAction) -> Self {
  240. onSelectedAction = action
  241. return self
  242. }
  243. /// Sets the onDeselectedAction
  244. ///
  245. /// - Parameter action: The new onDeselectedAction
  246. /// - Returns: Self
  247. @discardableResult
  248. open func onDeselected(_ action: @escaping InputBarButtonItemAction) -> Self {
  249. onDeselectedAction = action
  250. return self
  251. }
  252. /// Sets the onEnabledAction
  253. ///
  254. /// - Parameter action: The new onEnabledAction
  255. /// - Returns: Self
  256. @discardableResult
  257. open func onEnabled(_ action: @escaping InputBarButtonItemAction) -> Self {
  258. onEnabledAction = action
  259. return self
  260. }
  261. /// Sets the onDisabledAction
  262. ///
  263. /// - Parameter action: The new onDisabledAction
  264. /// - Returns: Self
  265. @discardableResult
  266. open func onDisabled(_ action: @escaping InputBarButtonItemAction) -> Self {
  267. onDisabledAction = action
  268. return self
  269. }
  270. // MARK: - InputItem Protocol
  271. /// Executes the onTextViewDidChangeAction with the given textView
  272. ///
  273. /// - Parameter textView: A reference to the InputTextView
  274. open func textViewDidChangeAction(with textView: InputTextView) {
  275. onTextViewDidChangeAction?(self, textView)
  276. }
  277. /// Executes the onKeyboardSwipeGestureAction with the given gesture
  278. ///
  279. /// - Parameter gesture: A reference to the gesture that was recognized
  280. open func keyboardSwipeGestureAction(with gesture: UISwipeGestureRecognizer) {
  281. onKeyboardSwipeGestureAction?(self, gesture)
  282. }
  283. /// Executes the onKeyboardEditingEndsAction
  284. open func keyboardEditingEndsAction() {
  285. onKeyboardEditingEndsAction?(self)
  286. }
  287. /// Executes the onKeyboardEditingBeginsAction
  288. open func keyboardEditingBeginsAction() {
  289. onKeyboardEditingBeginsAction?(self)
  290. }
  291. /// Executes the onTouchUpInsideAction
  292. @objc
  293. open func touchUpInsideAction() {
  294. onTouchUpInsideAction?(self)
  295. }
  296. // MARK: - Static Spacers
  297. /// An InputBarButtonItem that's spacing property is set to be .flexible
  298. public static var flexibleSpace: InputBarButtonItem {
  299. let item = InputBarButtonItem()
  300. item.setSize(.zero, animated: false)
  301. item.spacing = .flexible
  302. return item
  303. }
  304. /// An InputBarButtonItem that's spacing property is set to be .fixed with the width arguement
  305. public static func fixedSpace(_ width: CGFloat) -> InputBarButtonItem {
  306. let item = InputBarButtonItem()
  307. item.setSize(.zero, animated: false)
  308. item.spacing = .fixed(width)
  309. return item
  310. }
  311. }