import RxTheme
protocol Theme {
var backgroundColor: UIColor { get }
var textColor: UIColor { get }
}
struct LightTheme: Theme {
let backgroundColor = UIColor.white
let textColor = UIColor.black
}
struct DarkTheme: Theme {
let backgroundColor = UIColor.black
let textColor = UIColor.white
}
enum ThemeType: ThemeProvider {
case light, dark
var associatedObject: Theme {
switch self {
case .light:
return LightTheme()
case .dark:
return DarkTheme()
}
}
}
let themeService = ThemeType.service(initial: .light)
themeService.rx
.bind({ $0.textColor }, to: label.rx.textColor)
.bind({ $0.backgroundColor }, to: view.rx.backgroundColor)
.disposed(by: disposeBag)
themeService.set(.dark)
- backgroundColor
- tintColor
- tintColor
- titleColor
- font
- textColor
- highlightedTextColor
- shadowColor
- font
- keyboardAppearance
- textColor
- image
- progressTintColor
- trackTintColor
- pageIndicatorTintColor
- currentPageIndicatorTintColor
- tintColor
- thumbTintColor
- minimumTrackTintColor
- maximumTrackTintColor
- tintColor
- barTintColor
- onTintColor
- thumbTintColor
- separatorColor
- barStyle
- barTintColor
- titleTextAttributes
- barStyle
- barTintColor
- font
- textColor
- activityIndicatorViewStyle
- tintColor
- backgroundColor
- borderWidth
- borderColor
- shadowColor
Because RxTheme uses Binder<T>
from RxCocoa, any Binder
defined in RxCocoa could be used here.
This also makes the lib super easy to extend in your codebase, here is an example
extension Reactive where Base: UIView {
var borderColor: Binder<UIColor?> {
return Binder(self.base) { view, color in
view.layer.borderColor = color?.cgColor
}
}
}
Open codegen/exts.yml
, add class, attributes and supported os.
UILabel:
attrs:
font: UIFont
textColor: UIColor?
highlightedTextColor: UIColor?
shadowColor: UIColor?
os: [iOS, tvOS]
then run codegen script
// make sure you have python3 and pipenv installed
$ pipenv install
$ pipenv run python -m codegen
If you think it's commonly used, please send us a PR.
To run the example project, clone the repo, and run pod install
from the Example directory first.
pod 'RxTheme', '~> 2.0'
github "RxSwiftCommunity/RxTheme" ~> 2.0.0
duan, wddwyss@gmail.com
RxTheme is available under the MIT license. See the LICENSE file for more info.