Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 48 additions & 8 deletions Classes/Views/Not-MacOS/Button.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +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 {
super.setAttributedTitle(title, for: state)
return
if #available(iOS 14.0, *) {
guard let transition = titleChangeTransition else {
super.setAttributedTitle(title, for: state)
return
}
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
}
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)
}
UIView.transition(with: self, duration: 0.25, options: transition, animations: {
super.setAttributedTitle(title, for: state)
}, completion: nil)
}

@discardableResult
Expand Down Expand Up @@ -215,7 +239,23 @@ 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 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)
}
}
}

@discardableResult
public func color(_ color: UIColor, _ state: UIControl.State = .normal) -> Self {
setTitleColor(color, for: state)
Expand Down