Skip to content

Commit

Permalink
Lighter button fill in light mode (#489)
Browse files Browse the repository at this point in the history
Provides a lighter fill color for the button when in light mode and
handles updating it dynamically as the appearance changes.
  • Loading branch information
krugerk authored Oct 14, 2024
1 parent cf99e76 commit c751942
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions BeeKit/UI/BSButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,62 @@ public class BSButton : UIButton {

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)

registerForTraitChanges(
[UITraitUserInterfaceStyle.self]) {
(self: Self, previousTraitCollection: UITraitCollection) in
self.resetStyle()
}

self.setUp()
}

override init(frame: CGRect) {
super.init(frame: frame)

registerForTraitChanges(
[UITraitUserInterfaceStyle.self]) {
(self: Self, previousTraitCollection: UITraitCollection) in
self.resetStyle()
}

self.setUp()
}

private func setUp() {
self.titleLabel?.font = UIFont.beeminder.defaultBoldFont
self.setTitleColor(UIColor.Beeminder.yellow, for: UIControl.State())
self.tintColor = .black
self.setTitleColor(dynamicTitleColor, for: UIControl.State())
self.tintColor = dynamicTintFillColor
self.configuration = .filled()

self.layer.borderColor = UIColor.Beeminder.yellow.cgColor
self.layer.borderWidth = 1
self.layer.cornerRadius = 4
}

private func resetStyle() {
self.tintColor = dynamicTintFillColor
self.setTitleColor(dynamicTitleColor, for: UIControl.State())
}

private let dynamicTintFillColor = UIColor { traitCollection in
switch traitCollection.userInterfaceStyle {
case .dark:
return UIColor.black
default:
return UIColor(red: 235.0/255.0,
green: 235.0/255.0,
blue: 235.0/255.0,
alpha: 1.0)
}
}

private let dynamicTitleColor = UIColor { traitCollection in
switch traitCollection.userInterfaceStyle {
case .dark:
return UIColor.Beeminder.yellow
default:
return UIColor.black
}
}
}

0 comments on commit c751942

Please sign in to comment.