- Prebuilt UIKit controls with automatic theming
- Themes can be JSON or a struct
- Quick and easy to implement
- Flexible and straightforward to extend
To run the example project, clone the repo, and run pod install
from the Example directory first.
iOS 8 and Swift 4.2
EasyThemer is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'EasyThemer'
https://wfltaylor.github.io/EasyThemer/
There are two ways to declare a theme.
Themes can be declared by overriding ThemeDecleration
. This is the reccomended method for most uses.
class LightTheme: ThemeDeclaration {
override func setupProperties() {
titleFont = UIFont(name: "Gill Sans", size: 44)
primaryFont = UIFont(name: "Gill Sans", size: 12)
backgroundColor = UIColor.white
prefersLargeTitles = true
barStyle = UIBarStyle.default
}
}
You can also declare a theme by conforming to the Theme
protocol but the ThemeDecleration method is recomended due to the greater level of clarity.
You can also add custom properties to your theme like this:
class LightTheme: ThemeDeclaration {
var seperatorColor: UIColor
override func setupProperties() {
titleFont = UIFont(name: "Gill Sans", size: 44)
primaryFont = UIFont(name: "Gill Sans", size: 12)
backgroundColor = UIColor.white
prefersLargeTitles = true
barStyle = UIBarStyle.default
seperatorColor = UIColor.gray
}
}
Themes can be declared by using a JSON file. This isn't reccomended for local use but can be good for downloading themes from a server.
{
"backgroundColorCode" : {
"red" : 0,
"alpha" : 1,
"blue" : 0,
"green" : 1
},
"barStyle" : "default",
"primaryFontCode" : {
"name" : "Chalkboard SE",
"pointSize" : 12
},
"barTintColorCode" : {
"red" : 1,
"alpha" : 1,
"blue" : 0,
"green" : 0.6
},
"titleFontCode" : {
"name" : "Chalkboard SE",
"pointSize" : 54
},
"textColorCode" : {
"red" : 0,
"alpha" : 1,
"blue" : 0,
"green" : 0
},
"prefersLargeTitles" : true,
"tintColorCode" : {
"red" : 1,
"alpha" : 1,
"blue" : 0,
"green" : 0
}
}
If this was a file in the bundle, and called CrazyTheme.json
you could initialize using JSONTheme(named: "CrazyTheme")
To configure the ThemeEngine singleton you can put ThemeEngine.standard.configure(theme: LightTheme())
in your "did finish launching with options". You must provide a default theme.
ThemeEngine.standard.setTheme(theme: LightTheme())
ThemeEngine.standard.currentTheme!
The library includes some premade UIKit components including ThemeableNavigationBar
, ThemeableTabBar
, ThemeableLabel
, ThemeableButton
, and ThemeableView
.
These are design mostly for convenience whilst using Interface Builder and make assumptions about what you want applied to what part of the control.
To use, simply set the views Custom Class to one of the avaliable subclasses. These are automatically setup and will change themes with the standard ThemeEngine singleton.
The reccomended way to impliment custom theming is by using the ThemeEngineDelegate
.
extension MyViewController: ThemeEngineDelegate {
func themeChanged(theme: Theme) {
setTheme(theme: theme)
}
func themeReady(theme: Theme) {
setTheme(theme: theme)
}
func setTheme(theme: Theme) {
tableView.backgroundColor = theme.backgroundColor
}
func setupThemes() {
ThemeEngine.standard.register(self)
}
}
In this example, setupThemes()
is called in the viewDidLoad. It is important to not immediately set the theme in viewDidLoad()
or when initalizing because the ThemeEngine may not be configured yet - "did finish launching with options" is called after the Storyboard is loaded.
William Taylor
EasyThemer is available under the MIT license. See the LICENSE file for more info.