Latest 2.2.1
Homepage https://github.com/RxSwiftCommunity/RxTheme
License MIT
Platforms ios 9.0, tvos 9.0, osx 10.10, watchos 2.0, requires ARC
Dependencies RxSwift, RxCocoa
Authors

Build Status
Version
Carthage compatible
License
Platform

Manual

Define theme service

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)

Apply theme to UI

themeService.rx
    .bind({ $0.textColor }, to: label.rx.textColor)
    .bind({ $0.backgroundColor }, to: view.rx.backgroundColor)
    .disposed(by: disposeBag)

Switch themes

themeService.set(.dark)

Binder presets

UIView
  • backgroundColor
  • tintColor
UIButton
  • tintColor
  • titleColor
UILabel
  • font
  • textColor
  • highlightedTextColor
  • shadowColor
UITextField
  • font
  • keyboardAppearance
  • textColor
UIImageView
  • image
UIProgressView
  • progressTintColor
  • trackTintColor
UIPageControl
  • pageIndicatorTintColor
  • currentPageIndicatorTintColor
UISegmentedControl
  • tintColor
UISlider
  • thumbTintColor
  • minimumTrackTintColor
  • maximumTrackTintColor
UIToolbar
  • barTintColor
UISwitch
  • onTintColor
  • thumbTintColor
UITableView
  • separatorColor
UINavigationBar
  • barStyle
  • barTintColor
  • titleTextAttributes
UITabBar
  • barStyle
  • barTintColor
UITextView
  • font
  • textColor
UIActivityIndicatorView
  • style
UIBarButtonItem
  • tintColor
CALayer
  • backgroundColor
  • borderWidth
  • borderColor
  • shadowColor

Extend binders in your codebase

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
        }
    }
}

Extend binders in the lib

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.

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

Cocoapods

pod 'RxTheme', '~> 2.0'

Carthage

github "RxSwiftCommunity/RxTheme" ~> 2.0.0

Author

duan, [email protected]

License

RxTheme is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "RxTheme",
    "version": "2.2.1",
    "swift_version": "4.2",
    "summary": "Theme management based on RxCocoa",
    "description": "Theme management based on RxSwift and RxCocoa, easy to use, easy to extend.",
    "homepage": "https://github.com/RxSwiftCommunity/RxTheme",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "wddwycc": "[email protected]"
    },
    "source": {
        "git": "https://github.com/RxSwiftCommunity/RxTheme.git",
        "tag": "2.2.1"
    },
    "social_media_url": "https://twitter.com/wddwycc",
    "platforms": {
        "ios": "9.0",
        "tvos": "9.0",
        "osx": "10.10",
        "watchos": "2.0"
    },
    "requires_arc": true,
    "source_files": "RxTheme/Classes/**/*",
    "dependencies": {
        "RxSwift": [
            "~> 4.3"
        ],
        "RxCocoa": [
            "~> 4.3"
        ]
    }
}

Pin It on Pinterest

Share This