From 0cf49071e27d6c99fa5298e74e9fb33e19bcd8b1 Mon Sep 17 00:00:00 2001 From: Ekko Date: Thu, 26 May 2022 15:35:22 +0800 Subject: [PATCH 1/2] make setTitleColor work with title set of UButton, try to fix #38 --- Classes/Views/Not-MacOS/Button.swift | 33 +++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/Classes/Views/Not-MacOS/Button.swift b/Classes/Views/Not-MacOS/Button.swift index e34b8818..56703b1d 100644 --- a/Classes/Views/Not-MacOS/Button.swift +++ b/Classes/Views/Not-MacOS/Button.swift @@ -52,11 +52,25 @@ open class UButton: UIButton, AnyDeclarativeProtocol, DeclarativeProtocolInterna open override func setAttributedTitle(_ title: NSAttributedString?, for state: UIControl.State) { guard let transition = titleChangeTransition else { - super.setAttributedTitle(title, for: state) + if let title = title { + let color = self.titleColor(for: state) + let mutable = NSMutableAttributedString(attributedString: title) + mutable.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: mutable.length)) + super.setAttributedTitle(mutable, for: state) + } else { + super.setAttributedTitle(title, for: state) + } return } UIView.transition(with: self, duration: 0.25, options: transition, animations: { - super.setAttributedTitle(title, for: state) + if let title = title { + let color = self.titleColor(for: state) + let mutable = NSMutableAttributedString(attributedString: title) + mutable.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: mutable.length)) + super.setAttributedTitle(mutable, for: state) + } else { + super.setAttributedTitle(title, for: state) + } }, completion: nil) } @@ -215,7 +229,20 @@ open class UButton: UIButton, AnyDeclarativeProtocol, DeclarativeProtocolInterna } // MARK: Title Color - + open override func setTitleColor(_ color: UIColor?, for state: State) { + if #available(iOS 14.0, *) { + super.setTitleColor(color, for: state) + } + else { + let attibutedString = attributedTitle(for: state) + if let color = color, let attibutedString = attributedTitle(for: state) { + let mutable = NSMutableAttributedString(attributedString: attibutedString) + mutable.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: mutable.length)) + super.setAttributedTitle(mutable, for: state) + } + } + } + @discardableResult public func color(_ color: UIColor, _ state: UIControl.State = .normal) -> Self { setTitleColor(color, for: state) From 153d9ca55a94c88320120c62ec76a8accebca0fe Mon Sep 17 00:00:00 2001 From: EkkoG Date: Mon, 30 May 2022 23:21:22 +0800 Subject: [PATCH 2/2] [UButton] make setTitleColor works both iOS 14 above and below --- Classes/Views/Not-MacOS/Button.swift | 55 +++++++++++++++++----------- 1 file changed, 34 insertions(+), 21 deletions(-) diff --git a/Classes/Views/Not-MacOS/Button.swift b/Classes/Views/Not-MacOS/Button.swift index 56703b1d..af49b9e5 100644 --- a/Classes/Views/Not-MacOS/Button.swift +++ b/Classes/Views/Not-MacOS/Button.swift @@ -49,29 +49,39 @@ open class UButton: UIButton, AnyDeclarativeProtocol, DeclarativeProtocolInterna super.setTitle(title, for: state) }, completion: nil) } - + open override func setAttributedTitle(_ title: NSAttributedString?, for state: UIControl.State) { - guard let transition = titleChangeTransition else { - if let title = title { - let color = self.titleColor(for: state) - let mutable = NSMutableAttributedString(attributedString: title) - mutable.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: mutable.length)) - super.setAttributedTitle(mutable, for: state) - } else { + if #available(iOS 14.0, *) { + guard let transition = titleChangeTransition else { super.setAttributedTitle(title, for: state) + return } - return - } - UIView.transition(with: self, duration: 0.25, options: transition, animations: { - if let title = title { - let color = self.titleColor(for: state) - let mutable = NSMutableAttributedString(attributedString: title) - mutable.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: mutable.length)) - super.setAttributedTitle(mutable, for: state) - } else { + UIView.transition(with: self, duration: 0.25, options: transition, animations: { super.setAttributedTitle(title, for: state) + }, completion: nil) + } + else { + let color = self.titleColor(for: .normal) ?? .white + guard let transition = titleChangeTransition else { + if let title = title { + let mutable = NSMutableAttributedString(attributedString: title) + mutable.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: mutable.length)) + super.setAttributedTitle(mutable, for: state) + } else { + super.setAttributedTitle(title, for: state) + } + return } - }, completion: nil) + UIView.transition(with: self, duration: 0.25, options: transition, animations: { + if let title = title { + let mutable = NSMutableAttributedString(attributedString: title) + mutable.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: mutable.length)) + super.setAttributedTitle(mutable, for: state) + } else { + super.setAttributedTitle(title, for: state) + } + }, completion: nil) + } } @discardableResult @@ -234,12 +244,15 @@ open class UButton: UIButton, AnyDeclarativeProtocol, DeclarativeProtocolInterna super.setTitleColor(color, for: state) } else { - let attibutedString = attributedTitle(for: state) - if let color = color, let attibutedString = attributedTitle(for: state) { - let mutable = NSMutableAttributedString(attributedString: attibutedString) + let color = color ?? .white + if let attributedString = attributedTitle(for: state) { + let mutable = NSMutableAttributedString(attributedString: attributedString) mutable.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: mutable.length)) super.setAttributedTitle(mutable, for: state) } + else { + super.setTitleColor(color, for: state) + } } }