ButtonUtil.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. //
  2. // ButtonUtil.swift
  3. // HCQuanfangtong
  4. //
  5. // Created by Apple on 2022/1/21.
  6. // Copyright © 2022 Jyp. All rights reserved.
  7. //
  8. import Foundation
  9. import RxSwift
  10. import RxCocoa
  11. import UIKit
  12. //MARK: - 扩展 selected 和 normal 状态下的borderlayer 和 borderColor 以及 backgroundColor
  13. extension UIButton{
  14. open override func awakeFromNib() {
  15. super.awakeFromNib()
  16. self.setOtherCustomStyle()
  17. }
  18. @objc func setOtherCustomStyle(){
  19. _ = Observable.of(self.rx.methodInvoked(#selector(setter: self.isSelected)),
  20. self.rx.methodInvoked(#selector(setter: self.isHighlighted)),
  21. self.rx.methodInvoked(#selector(setter: self.isEnabled))).merge().takeUntil(self.rx.deallocated).subscribe(onNext:{[unowned self]
  22. _ in
  23. self.setStatusConfig()
  24. })
  25. }
  26. private func setStatusConfig(){
  27. if !self.isEnabled{
  28. if self.disabledBackgroundColor.isNotNil{
  29. self.backgroundColor = self.disabledBackgroundColor
  30. }
  31. if self.disabledLayerBorderColor.isNotNil{
  32. self.layer.borderColor = self.disabledLayerBorderColor?.cgColor
  33. }
  34. if self.disabledLayerBorderWidth.isNotNil{
  35. self.layer.borderWidth = CGFloat.init(Convert.toFloat(self.disabledLayerBorderWidth) ?? 0)
  36. }
  37. }
  38. else{
  39. if self.isHighlighted{
  40. if self.highlightedBackgroundColor.isNotNil{
  41. self.backgroundColor = self.highlightedBackgroundColor
  42. }
  43. if self.highlightedLayerBorderColor.isNotNil{
  44. self.layer.borderColor = self.highlightedLayerBorderColor?.cgColor
  45. }
  46. if self.highlightedLayerBorderWidth.isNotNil{
  47. self.layer.borderWidth = CGFloat.init(Convert.toFloat(self.highlightedLayerBorderWidth) ?? 0)
  48. }
  49. }
  50. else{
  51. if self.isSelected{
  52. if self.selectedBackgroundColor.isNotNil{
  53. self.backgroundColor = self.selectedBackgroundColor
  54. }
  55. if self.selectedLayerBorderColor.isNotNil{
  56. self.layer.borderColor = self.selectedLayerBorderColor?.cgColor
  57. }
  58. if self.selectedLayerBorderWidth.isNotNil{
  59. self.layer.borderWidth = CGFloat.init(Convert.toFloat(self.selectedLayerBorderWidth) ?? 0)
  60. }
  61. }
  62. else{
  63. if self.normalBackgroundColor.isNotNil{
  64. self.backgroundColor = self.normalBackgroundColor
  65. }
  66. if self.normalLayerBorderColor.isNotNil{
  67. self.layer.borderColor = self.normalLayerBorderColor?.cgColor
  68. }
  69. if self.normalLayerBorderWidth.isNotNil{
  70. self.layer.borderWidth = CGFloat.init(Convert.toFloat(self.normalLayerBorderWidth) ?? 0)
  71. }
  72. }
  73. }
  74. }
  75. }
  76. struct AssosiatedKey{
  77. static var selectedLayerBorderColor = "selectedLayerColor"
  78. static var selectedLayerBorderWidth = "selectedLayerBorderWidth"
  79. static var selectedBackgroundColor = "selectedBackgroundColor"
  80. static var normalLayerBorderColor = "normalLayerBorderColor"
  81. static var normalLayerBorderWidth = "normalLayerBorderWidth"
  82. static var normalBackgroundColor = "normalBackgroundColor"
  83. static var disabledLayerBorderColor = "disabledLayerBorderColor"
  84. static var disabledLayerBorderWidth = "disabledLayerBorderWidth"
  85. static var disabledBackgroundColor = "disabledBackgroundColor"
  86. static var highlightedLayerBorderColor = "highlightedLayerBorderColor"
  87. static var highlightedLayerBorderWidth = "selectedLayerBorderWidth"
  88. static var highlightedBackgroundColor = "selectedBackgroundColor"
  89. }
  90. @objc var selectedLayerBorderColor : UIColor?{
  91. set{
  92. objc_setAssociatedObject(self, &(AssosiatedKey.selectedLayerBorderColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  93. if self.isEnabled && !self.isHighlighted && self.isSelected && newValue.isNotNil{
  94. self.layer.borderColor = newValue?.cgColor
  95. }
  96. }
  97. get{
  98. objc_getAssociatedObject(self, &(AssosiatedKey.selectedLayerBorderColor)) as? UIColor
  99. }
  100. }
  101. @objc var selectedLayerBorderWidth : NSString?{
  102. set{
  103. objc_setAssociatedObject(self, &(AssosiatedKey.selectedLayerBorderWidth), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  104. if self.isEnabled && !self.isHighlighted && self.isSelected && newValue.isNotNil{
  105. self.layer.borderWidth = CGFloat.init(Convert.toFloat(newValue) ?? 0)
  106. }
  107. }
  108. get{
  109. objc_getAssociatedObject(self, &(AssosiatedKey.selectedLayerBorderWidth)) as? NSString
  110. }
  111. }
  112. @objc var selectedBackgroundColor : UIColor?{
  113. set{
  114. objc_setAssociatedObject(self, &(AssosiatedKey.selectedBackgroundColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  115. if self.isEnabled && !self.isHighlighted && self.isSelected && newValue.isNotNil{
  116. self.backgroundColor = newValue
  117. }
  118. }
  119. get{
  120. objc_getAssociatedObject(self, &(AssosiatedKey.selectedBackgroundColor)) as? UIColor
  121. }
  122. }
  123. @objc var normalLayerBorderColor : UIColor?{
  124. set{
  125. objc_setAssociatedObject(self, &(AssosiatedKey.normalLayerBorderColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  126. if self.isEnabled && !self.isHighlighted && !self.isSelected && newValue.isNotNil{
  127. self.layer.borderColor = newValue?.cgColor
  128. }
  129. }
  130. get{
  131. objc_getAssociatedObject(self, &(AssosiatedKey.normalLayerBorderColor)) as? UIColor
  132. }
  133. }
  134. @objc var normalLayerBorderWidth : NSString?{
  135. set{
  136. objc_setAssociatedObject(self, &(AssosiatedKey.normalLayerBorderWidth), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  137. if self.isEnabled && !self.isHighlighted && !self.isSelected && newValue.isNotNil{
  138. self.layer.borderWidth = CGFloat.init(Convert.toFloat(newValue) ?? 0)
  139. }
  140. }
  141. get{
  142. objc_getAssociatedObject(self, &(AssosiatedKey.normalLayerBorderWidth)) as? NSString
  143. }
  144. }
  145. @objc var normalBackgroundColor : UIColor?{
  146. set{
  147. objc_setAssociatedObject(self, &(AssosiatedKey.normalBackgroundColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  148. if self.isEnabled && !self.isHighlighted && !self.isSelected && newValue.isNotNil{
  149. self.backgroundColor = newValue
  150. }
  151. }
  152. get{
  153. objc_getAssociatedObject(self, &(AssosiatedKey.normalBackgroundColor)) as? UIColor
  154. }
  155. }
  156. @objc var disabledLayerBorderColor : UIColor?{
  157. set{
  158. objc_setAssociatedObject(self, &(AssosiatedKey.disabledLayerBorderColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  159. if !self.isEnabled && newValue.isNotNil{
  160. self.layer.borderColor = newValue?.cgColor
  161. }
  162. }
  163. get{
  164. objc_getAssociatedObject(self, &(AssosiatedKey.disabledLayerBorderColor)) as? UIColor
  165. }
  166. }
  167. @objc var disabledLayerBorderWidth : NSString?{
  168. set{
  169. objc_setAssociatedObject(self, &(AssosiatedKey.disabledLayerBorderWidth), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  170. if !self.isEnabled && newValue.isNotNil{
  171. self.layer.borderWidth = CGFloat.init(Convert.toFloat(newValue) ?? 0)
  172. }
  173. }
  174. get{
  175. objc_getAssociatedObject(self, &(AssosiatedKey.disabledLayerBorderWidth)) as? NSString
  176. }
  177. }
  178. @objc var disabledBackgroundColor : UIColor?{
  179. set{
  180. objc_setAssociatedObject(self, &(AssosiatedKey.disabledBackgroundColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  181. if !self.isEnabled && newValue.isNotNil{
  182. self.backgroundColor = newValue
  183. }
  184. }
  185. get{
  186. objc_getAssociatedObject(self, &(AssosiatedKey.disabledBackgroundColor)) as? UIColor
  187. }
  188. }
  189. @objc var highlightedLayerBorderColor : UIColor?{
  190. set{
  191. objc_setAssociatedObject(self, &(AssosiatedKey.highlightedLayerBorderColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  192. if self.isEnabled && self.isHighlighted && newValue.isNotNil{
  193. self.layer.borderColor = newValue?.cgColor
  194. }
  195. }
  196. get{
  197. objc_getAssociatedObject(self, &(AssosiatedKey.highlightedLayerBorderColor)) as? UIColor
  198. }
  199. }
  200. @objc var highlightedLayerBorderWidth : NSString?{
  201. set{
  202. objc_setAssociatedObject(self, &(AssosiatedKey.highlightedLayerBorderWidth), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  203. if self.isEnabled && self.isHighlighted && newValue.isNotNil{
  204. self.layer.borderWidth = CGFloat.init(Convert.toFloat(newValue) ?? 0)
  205. }
  206. }
  207. get{
  208. objc_getAssociatedObject(self, &(AssosiatedKey.highlightedLayerBorderWidth)) as? NSString
  209. }
  210. }
  211. @objc var highlightedBackgroundColor : UIColor?{
  212. set{
  213. objc_setAssociatedObject(self, &(AssosiatedKey.highlightedBackgroundColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  214. if self.isEnabled && self.isHighlighted && newValue.isNotNil{
  215. self.backgroundColor = newValue
  216. }
  217. }
  218. get{
  219. objc_getAssociatedObject(self, &(AssosiatedKey.highlightedBackgroundColor)) as? UIColor
  220. }
  221. }
  222. @objc func setBorderColor(_ borderColor: UIColor, forState state: UIControl.State){
  223. if state == .selected{
  224. self.selectedLayerBorderColor = borderColor
  225. }
  226. else if state == .disabled{
  227. self.disabledLayerBorderColor = borderColor
  228. }
  229. else if state == .highlighted{
  230. self.highlightedLayerBorderColor = borderColor
  231. }
  232. else{
  233. self.normalLayerBorderColor = borderColor
  234. if self.selectedLayerBorderColor.isNil{
  235. self.setBorderColor(borderColor, forState: .selected)
  236. }
  237. if self.highlightedLayerBorderColor.isNil{
  238. self.setBorderColor(borderColor, forState: .highlighted)
  239. }
  240. if self.disabledLayerBorderColor.isNil{
  241. self.setBorderColor(borderColor, forState: .disabled)
  242. }
  243. }
  244. }
  245. @objc func setBorderWidth(_ borderWidth: NSString, forState state: UIControl.State){
  246. if state == .selected{
  247. self.selectedLayerBorderWidth = borderWidth
  248. }
  249. else if state == .disabled{
  250. self.disabledLayerBorderWidth = borderWidth
  251. }
  252. else if state == .highlighted{
  253. self.highlightedLayerBorderWidth = borderWidth
  254. }
  255. else{
  256. self.normalLayerBorderWidth = borderWidth
  257. if self.selectedLayerBorderWidth.isNil{
  258. self.setBorderWidth(borderWidth, forState: .selected)
  259. }
  260. if self.highlightedLayerBorderWidth.isNil{
  261. self.setBorderWidth(borderWidth, forState: .highlighted)
  262. }
  263. if self.disabledLayerBorderWidth.isNil{
  264. self.setBorderWidth(borderWidth, forState: .disabled)
  265. }
  266. }
  267. }
  268. @objc func setBackColor(_ backgroundColor: UIColor, forState state: UIControl.State){
  269. if state == .selected{
  270. self.selectedBackgroundColor = backgroundColor
  271. }
  272. else if state == .disabled{
  273. self.disabledBackgroundColor = backgroundColor
  274. }
  275. else if state == .highlighted{
  276. self.highlightedBackgroundColor = backgroundColor
  277. }
  278. else{
  279. self.normalBackgroundColor = backgroundColor
  280. if self.selectedBackgroundColor.isNil{
  281. self.setBackColor(backgroundColor, forState: .selected)
  282. }
  283. if self.highlightedBackgroundColor.isNil{
  284. self.setBackColor(backgroundColor, forState: .highlighted)
  285. }
  286. if self.disabledBackgroundColor.isNil{
  287. self.setBackColor(backgroundColor, forState: .disabled)
  288. }
  289. }
  290. }
  291. public class func createButton(withTitle title: String?, titleColor: UIColor?, font: UIFont?, image: UIImage?, tap tapAction: (()->Void)?) -> Self{
  292. let btn = Self.init(type: ButtonType.custom)
  293. btn.setTitle(title, for: UIControl.State.normal)
  294. btn.setTitleColor(titleColor, for: UIControl.State.normal)
  295. btn.setImage(image, for: UIControl.State.normal)
  296. btn.titleLabel?.font = font
  297. _ = btn.rx.tap.takeUntil(btn.rx.deallocated).subscribe(onNext:{
  298. _ in
  299. tapAction?()
  300. })
  301. return btn
  302. }
  303. }
  304. extension UIButton{
  305. func setTitleAndImageDistance(_ distance: CGFloat){
  306. if self.image(for: .normal).isNotNil && self.title(for: .normal).isNotNil{
  307. if self.semanticContentAttribute == .forceRightToLeft{
  308. self.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -distance * 0.5, bottom: 0, right: distance * 0.5)
  309. self.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: distance * 0.5, bottom: 0, right: -distance * 0.5)
  310. }
  311. else{
  312. self.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: distance * 0.5, bottom: 0, right: -distance * 0.5)
  313. self.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: -distance * 0.5, bottom: 0, right: distance * 0.5)
  314. }
  315. }
  316. }
  317. }
  318. //MARK: - 增加一些binder
  319. extension Reactive where Base : UIButton{
  320. // /// Reactive wrapper for `addTarget:`
  321. // public var tapBinder: Binder<(()->Void)?> {
  322. // return Binder(self.base) { button, tapBinder in
  323. // button.rac_signal(for: UIControl.Event.touchUpInside).subscribeNext { _ in
  324. // tapBinder?()
  325. // }
  326. // }
  327. // }
  328. //
  329. // /// Reactive wrapper for `setTitleColor(_:for:)`
  330. // public func color(for controlState: UIControlState = []) -> Binder<String?> {
  331. // return Binder(self.base) { button, color -> Void in
  332. // button.setTitleColor(UIColor.init(macHexString: color ?? ""), for: controlState)
  333. // }
  334. // }
  335. /// Reactive wrapper for `setImage(_:for:)`
  336. public func imageNamed(for controlState : UIControl.State = []) -> Binder<String?>{
  337. return Binder(self.base) { button, imageNamed -> Void in
  338. button.setImage(UIImage.init(named: imageNamed ?? ""), for: controlState)
  339. }
  340. }
  341. /// Reactive wrapper for `setBackgroundImage(_:for:)`
  342. public func backImageNamed(for controlState : UIControl.State = []) -> Binder<String?>{
  343. return Binder(self.base) { button, imageNamed -> Void in
  344. button.setBackgroundImage(UIImage.init(named: imageNamed ?? ""), for: controlState)
  345. }
  346. }
  347. /// Reactive wrapper for `titleLabel.font`
  348. public var fontBinder : Binder<UIFont?>{
  349. return Binder(self.base) {button , font in
  350. button.titleLabel?.font = font
  351. }
  352. }
  353. }
  354. extension Reactive where Base: UIControl{
  355. public var shareTap: Observable<Void>{
  356. get{
  357. if self.base.controlEventTouchUpInside.isNil{
  358. self.base.controlEventTouchUpInside = self.base.rx.controlEvent(.touchUpInside).share()
  359. }
  360. return self.base.controlEventTouchUpInside!
  361. }
  362. }
  363. }
  364. extension UIControl{
  365. static private var associatedKeyOfControlEventTouchUpInside: String = "KControlEventTouchUpInside"
  366. var controlEventTouchUpInside: Observable<Void>?{
  367. set{
  368. objc_setAssociatedObject(self, &UIControl.associatedKeyOfControlEventTouchUpInside, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  369. }
  370. get{
  371. objc_getAssociatedObject(self, &UIControl.associatedKeyOfControlEventTouchUpInside) as? Observable<Void>
  372. }
  373. }
  374. }