// // 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 { // 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{ 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{ 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{ return Binder(self.base) {button , font in button.titleLabel?.font = font } } } extension Reactive where Base: UIControl{ public var shareTap: Observable{ 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?{ set{ objc_setAssociatedObject(self, &UIControl.associatedKeyOfControlEventTouchUpInside, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC) } get{ objc_getAssociatedObject(self, &UIControl.associatedKeyOfControlEventTouchUpInside) as? Observable } } }