123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425 |
- //
- // ButtonUtil.swift
- // HCQuanfangtong
- //
- // Created by Apple on 2022/1/21.
- // Copyright © 2022 Jyp. All rights reserved.
- //
- import Foundation
- import RxSwift
- import RxCocoa
- import UIKit
- //MARK: - 扩展 selected 和 normal 状态下的borderlayer 和 borderColor 以及 backgroundColor
- extension UIButton{
-
- open override func awakeFromNib() {
- super.awakeFromNib()
- self.setOtherCustomStyle()
- }
- @objc func setOtherCustomStyle(){
-
- _ = Observable.of(self.rx.methodInvoked(#selector(setter: self.isSelected)),
- self.rx.methodInvoked(#selector(setter: self.isHighlighted)),
- self.rx.methodInvoked(#selector(setter: self.isEnabled))).merge().takeUntil(self.rx.deallocated).subscribe(onNext:{[unowned self]
- _ in
- self.setStatusConfig()
- })
- }
-
- private func setStatusConfig(){
- if !self.isEnabled{
- if self.disabledBackgroundColor.isNotNil{
- self.backgroundColor = self.disabledBackgroundColor
- }
- if self.disabledLayerBorderColor.isNotNil{
- self.layer.borderColor = self.disabledLayerBorderColor?.cgColor
- }
- if self.disabledLayerBorderWidth.isNotNil{
- self.layer.borderWidth = CGFloat.init(Convert.toFloat(self.disabledLayerBorderWidth) ?? 0)
- }
- }
- else{
- if self.isHighlighted{
- if self.highlightedBackgroundColor.isNotNil{
- self.backgroundColor = self.highlightedBackgroundColor
- }
- if self.highlightedLayerBorderColor.isNotNil{
- self.layer.borderColor = self.highlightedLayerBorderColor?.cgColor
- }
- if self.highlightedLayerBorderWidth.isNotNil{
- self.layer.borderWidth = CGFloat.init(Convert.toFloat(self.highlightedLayerBorderWidth) ?? 0)
- }
- }
- else{
- if self.isSelected{
- if self.selectedBackgroundColor.isNotNil{
- self.backgroundColor = self.selectedBackgroundColor
- }
- if self.selectedLayerBorderColor.isNotNil{
- self.layer.borderColor = self.selectedLayerBorderColor?.cgColor
- }
- if self.selectedLayerBorderWidth.isNotNil{
- self.layer.borderWidth = CGFloat.init(Convert.toFloat(self.selectedLayerBorderWidth) ?? 0)
- }
- }
- else{
- if self.normalBackgroundColor.isNotNil{
- self.backgroundColor = self.normalBackgroundColor
- }
- if self.normalLayerBorderColor.isNotNil{
- self.layer.borderColor = self.normalLayerBorderColor?.cgColor
- }
- if self.normalLayerBorderWidth.isNotNil{
- self.layer.borderWidth = CGFloat.init(Convert.toFloat(self.normalLayerBorderWidth) ?? 0)
- }
- }
- }
- }
- }
-
-
-
- struct AssosiatedKey{
- static var selectedLayerBorderColor = "selectedLayerColor"
- static var selectedLayerBorderWidth = "selectedLayerBorderWidth"
- static var selectedBackgroundColor = "selectedBackgroundColor"
- static var normalLayerBorderColor = "normalLayerBorderColor"
- static var normalLayerBorderWidth = "normalLayerBorderWidth"
- static var normalBackgroundColor = "normalBackgroundColor"
- static var disabledLayerBorderColor = "disabledLayerBorderColor"
- static var disabledLayerBorderWidth = "disabledLayerBorderWidth"
- static var disabledBackgroundColor = "disabledBackgroundColor"
- static var highlightedLayerBorderColor = "highlightedLayerBorderColor"
- static var highlightedLayerBorderWidth = "selectedLayerBorderWidth"
- static var highlightedBackgroundColor = "selectedBackgroundColor"
- }
-
-
- @objc var selectedLayerBorderColor : UIColor?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.selectedLayerBorderColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
-
- if self.isEnabled && !self.isHighlighted && self.isSelected && newValue.isNotNil{
- self.layer.borderColor = newValue?.cgColor
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.selectedLayerBorderColor)) as? UIColor
- }
- }
- @objc var selectedLayerBorderWidth : NSString?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.selectedLayerBorderWidth), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- if self.isEnabled && !self.isHighlighted && self.isSelected && newValue.isNotNil{
- self.layer.borderWidth = CGFloat.init(Convert.toFloat(newValue) ?? 0)
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.selectedLayerBorderWidth)) as? NSString
- }
- }
-
- @objc var selectedBackgroundColor : UIColor?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.selectedBackgroundColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- if self.isEnabled && !self.isHighlighted && self.isSelected && newValue.isNotNil{
- self.backgroundColor = newValue
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.selectedBackgroundColor)) as? UIColor
- }
- }
-
- @objc var normalLayerBorderColor : UIColor?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.normalLayerBorderColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- if self.isEnabled && !self.isHighlighted && !self.isSelected && newValue.isNotNil{
- self.layer.borderColor = newValue?.cgColor
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.normalLayerBorderColor)) as? UIColor
- }
- }
- @objc var normalLayerBorderWidth : NSString?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.normalLayerBorderWidth), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- if self.isEnabled && !self.isHighlighted && !self.isSelected && newValue.isNotNil{
- self.layer.borderWidth = CGFloat.init(Convert.toFloat(newValue) ?? 0)
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.normalLayerBorderWidth)) as? NSString
- }
- }
-
- @objc var normalBackgroundColor : UIColor?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.normalBackgroundColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- if self.isEnabled && !self.isHighlighted && !self.isSelected && newValue.isNotNil{
- self.backgroundColor = newValue
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.normalBackgroundColor)) as? UIColor
- }
- }
-
- @objc var disabledLayerBorderColor : UIColor?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.disabledLayerBorderColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- if !self.isEnabled && newValue.isNotNil{
- self.layer.borderColor = newValue?.cgColor
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.disabledLayerBorderColor)) as? UIColor
- }
- }
- @objc var disabledLayerBorderWidth : NSString?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.disabledLayerBorderWidth), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- if !self.isEnabled && newValue.isNotNil{
- self.layer.borderWidth = CGFloat.init(Convert.toFloat(newValue) ?? 0)
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.disabledLayerBorderWidth)) as? NSString
- }
- }
-
- @objc var disabledBackgroundColor : UIColor?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.disabledBackgroundColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- if !self.isEnabled && newValue.isNotNil{
- self.backgroundColor = newValue
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.disabledBackgroundColor)) as? UIColor
- }
- }
-
- @objc var highlightedLayerBorderColor : UIColor?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.highlightedLayerBorderColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- if self.isEnabled && self.isHighlighted && newValue.isNotNil{
- self.layer.borderColor = newValue?.cgColor
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.highlightedLayerBorderColor)) as? UIColor
- }
- }
- @objc var highlightedLayerBorderWidth : NSString?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.highlightedLayerBorderWidth), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- if self.isEnabled && self.isHighlighted && newValue.isNotNil{
- self.layer.borderWidth = CGFloat.init(Convert.toFloat(newValue) ?? 0)
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.highlightedLayerBorderWidth)) as? NSString
- }
- }
-
- @objc var highlightedBackgroundColor : UIColor?{
- set{
- objc_setAssociatedObject(self, &(AssosiatedKey.highlightedBackgroundColor), newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- if self.isEnabled && self.isHighlighted && newValue.isNotNil{
- self.backgroundColor = newValue
- }
- }
- get{
- objc_getAssociatedObject(self, &(AssosiatedKey.highlightedBackgroundColor)) as? UIColor
- }
- }
-
-
- @objc func setBorderColor(_ borderColor: UIColor, forState state: UIControl.State){
-
- if state == .selected{
- self.selectedLayerBorderColor = borderColor
- }
- else if state == .disabled{
- self.disabledLayerBorderColor = borderColor
- }
- else if state == .highlighted{
- self.highlightedLayerBorderColor = borderColor
- }
- else{
- self.normalLayerBorderColor = borderColor
- if self.selectedLayerBorderColor.isNil{
- self.setBorderColor(borderColor, forState: .selected)
- }
- if self.highlightedLayerBorderColor.isNil{
- self.setBorderColor(borderColor, forState: .highlighted)
- }
- if self.disabledLayerBorderColor.isNil{
- self.setBorderColor(borderColor, forState: .disabled)
- }
- }
- }
-
- @objc func setBorderWidth(_ borderWidth: NSString, forState state: UIControl.State){
- if state == .selected{
- self.selectedLayerBorderWidth = borderWidth
- }
- else if state == .disabled{
- self.disabledLayerBorderWidth = borderWidth
- }
- else if state == .highlighted{
- self.highlightedLayerBorderWidth = borderWidth
- }
- else{
- self.normalLayerBorderWidth = borderWidth
- if self.selectedLayerBorderWidth.isNil{
- self.setBorderWidth(borderWidth, forState: .selected)
- }
- if self.highlightedLayerBorderWidth.isNil{
- self.setBorderWidth(borderWidth, forState: .highlighted)
- }
- if self.disabledLayerBorderWidth.isNil{
- self.setBorderWidth(borderWidth, forState: .disabled)
- }
- }
- }
-
- @objc func setBackColor(_ backgroundColor: UIColor, forState state: UIControl.State){
- if state == .selected{
- self.selectedBackgroundColor = backgroundColor
- }
- else if state == .disabled{
- self.disabledBackgroundColor = backgroundColor
- }
- else if state == .highlighted{
- self.highlightedBackgroundColor = backgroundColor
- }
- else{
- self.normalBackgroundColor = backgroundColor
- if self.selectedBackgroundColor.isNil{
- self.setBackColor(backgroundColor, forState: .selected)
- }
- if self.highlightedBackgroundColor.isNil{
- self.setBackColor(backgroundColor, forState: .highlighted)
- }
- if self.disabledBackgroundColor.isNil{
- self.setBackColor(backgroundColor, forState: .disabled)
- }
- }
- }
-
-
-
-
- public class func createButton(withTitle title: String?, titleColor: UIColor?, font: UIFont?, image: UIImage?, tap tapAction: (()->Void)?) -> Self{
- let btn = Self.init(type: ButtonType.custom)
- btn.setTitle(title, for: UIControl.State.normal)
- btn.setTitleColor(titleColor, for: UIControl.State.normal)
- btn.setImage(image, for: UIControl.State.normal)
- btn.titleLabel?.font = font
- _ = btn.rx.tap.takeUntil(btn.rx.deallocated).subscribe(onNext:{
- _ in
- tapAction?()
- })
- return btn
- }
-
- }
- extension UIButton{
-
- func setTitleAndImageDistance(_ distance: CGFloat){
-
- if self.image(for: .normal).isNotNil && self.title(for: .normal).isNotNil{
- if self.semanticContentAttribute == .forceRightToLeft{
- self.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: -distance * 0.5, bottom: 0, right: distance * 0.5)
- self.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: distance * 0.5, bottom: 0, right: -distance * 0.5)
- }
- else{
- self.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: distance * 0.5, bottom: 0, right: -distance * 0.5)
- self.imageEdgeInsets = UIEdgeInsets.init(top: 0, left: -distance * 0.5, bottom: 0, right: distance * 0.5)
- }
- }
- }
-
- }
- //MARK: - 增加一些binder
- extension Reactive where Base : UIButton{
- // /// Reactive wrapper for `addTarget:`
- // public var tapBinder: Binder<(()->Void)?> {
- // return Binder(self.base) { button, tapBinder in
- // button.rac_signal(for: UIControl.Event.touchUpInside).subscribeNext { _ in
- // tapBinder?()
- // }
- // }
- // }
- //
- // /// Reactive wrapper for `setTitleColor(_:for:)`
- // public func color(for controlState: UIControlState = []) -> Binder<String?> {
- // return Binder(self.base) { button, color -> Void in
- // button.setTitleColor(UIColor.init(macHexString: color ?? ""), for: controlState)
- // }
- // }
-
- /// Reactive wrapper for `setImage(_:for:)`
- public func imageNamed(for controlState : UIControl.State = []) -> Binder<String?>{
- return Binder(self.base) { button, imageNamed -> Void in
- button.setImage(UIImage.init(named: imageNamed ?? ""), for: controlState)
- }
- }
-
- /// Reactive wrapper for `setBackgroundImage(_:for:)`
- public func backImageNamed(for controlState : UIControl.State = []) -> Binder<String?>{
- return Binder(self.base) { button, imageNamed -> Void in
- button.setBackgroundImage(UIImage.init(named: imageNamed ?? ""), for: controlState)
- }
- }
-
- /// Reactive wrapper for `titleLabel.font`
- public var fontBinder : Binder<UIFont?>{
- return Binder(self.base) {button , font in
- button.titleLabel?.font = font
- }
- }
-
- }
- extension Reactive where Base: UIControl{
- public var shareTap: Observable<Void>{
- get{
- if self.base.controlEventTouchUpInside.isNil{
- self.base.controlEventTouchUpInside = self.base.rx.controlEvent(.touchUpInside).share()
- }
- return self.base.controlEventTouchUpInside!
- }
- }
- }
- extension UIControl{
- static private var associatedKeyOfControlEventTouchUpInside: String = "KControlEventTouchUpInside"
- var controlEventTouchUpInside: Observable<Void>?{
- set{
- objc_setAssociatedObject(self, &UIControl.associatedKeyOfControlEventTouchUpInside, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
- }
- get{
- objc_getAssociatedObject(self, &UIControl.associatedKeyOfControlEventTouchUpInside) as? Observable<Void>
- }
- }
-
- }
|