Themify is a small library that manages color themes using appearance proxies. The ideia is to create a standardized, centralized and simple way to create color themes that are applied to your app.
Creating color themes for an app is a boring task. You need to set color properties over and over again, using storyboards or xib files. For large apps, changing your color theme is hard and a no-brainer task.
If you decide to use appearance proxies, you find yourself writing the same boiler-plate code over and over again.
Themify try to centralize it by using definitions stored in plist files. So, you can change the entire look of your app from a central point, without having to edit any interface files anymore.
Themify does not rely on method swizzling like other libraries do. It uses the default way to customize apps by using appearance proxies. This library get the class name from your configuration, locate it on the run-time and do its magic.
It is designed to be small, fast and easy to use.
Your application don´t have access to the theme structure. It is maintained internally. Themify main class is just a theme manager, loading and applying it when requested. Since a theme is a shared state of any application, Themify class was implemented as a singleton.
The theme file is a simple plist. However, this plist must have the following format:
- Array (top-level)
- Dictionary
- name: String - Theme name
- elements: Array (element definitions)
- Dictionary (element definition)
- element: String - Name of the element (i.e., UILabel)
- attribute: String - Attribute to customize. (the key is the attribute, and the value, what is being customized
- Dictionary (element definition)
- Dictionary
See the file TestTheme.plist for a practical example.
Usage is quite simple. Themes are identified by meaningful names in plist file. Here is a plist example:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>name</key>
<string>Default</string>
<key>elements</key>
<array>
<dict>
<key>element</key>
<string>UITabBar</string>
<key>tintColor</key>
<string>#FF8000</string>
</dict>
<dict>
<key>element</key>
<string>UINavigationBar</string>
<key>tintColor</key>
<string>#FF8000</string>
</dict>
</array>
</dict>
</array>
</plist>
Each plist contains several themes. That's why it holds an array as top-level element. So, for this example, here is how to load this theme, in swift:
import Themify
if let themeURL = Bundle.main.url(forResource: "theme", withExtension: "plist") {
do {
try Themify.shared.loadThemes(from: themeURL)
try Themify.shared.applyTheme(themeName: "Default")
} catch {
// Catch exception here.
}
}
This library is released under MIT License.