Introduction - Demos - Installation - Documents - FAQ - Contribution - 中文文档
Running:open
SwiftTheme.xcworkspace
, run targetPlistDemo
As part of our project requirement, we need to add night mode to our app 节操精选. It's not as simple as just changing brightness or alpha on the top-level view—in fact, it needs an entirely new interface: different colors, different alpha, different image cuts. More accurately, "night mode" is a theme/skinning feature that can switch between bright theme and dark themes.
So how do we achieve this? Maybe we can set a global variable that represents the currently selected theme, and use different background colors or image cuts based on the variable during the controller's initialization. But then how do we deal with views that have already been initialized? Yes, we could use notifications to change their colors or image cuts, but this leads to controllers unnecessarily full of notification register/unregister, if...else and UI updating code. Worse, if you forget to unregister the notifications, your app may crash.
After some consideration, we put forward higher requirements on the task: create a simple and reusable themes/skinning framework, here as you see.
Make SwiftTheme a simple, powerful, high-performance, extensible themes/skinning framework. Provide a unified solution for iOS.
Vary background color of UIView according to the theme setting:
view.themeBackgroundColor = ["#FFF", "#000"]
Vary text color of UILable and UIButton:
label.themeTextColor = ["#000", "#FFF"]
button.themeSetTitleColor(["#000", "#FFF"], forState: .Normal)
Vary image of UIImageView:
imageView.themeImage = ["day", "night"]
// It's ok by using UIImage instances if you don't want to use image names.
imageView.themeImage = ThemeImagePicker(images: image1, image2)
A miracle happens after you execute the single line of code below!
// these numbers represent the parameters' index.
// eg. "view.themeBackgroundColor = ["#FFF", "#000"]", index 0 represents "#FFF", index 1 represents "#000"
ThemeManager.setTheme(index: isNight ? 1 : 0)
Get current theme index.
ThemeManager.currentThemeIndex // Readonly
Index mode is a fast way for the situation: a few themes, but not many, no need to download more new themes.
Notice About Literal:
// Wrong example:
let colors = ["#FFF", "#000"]
view.themeBackgroundColor = colors
// You should write like this:
view.themeBackgroundColor = ["#FFF", "#000"]
// or this:
let colorPickers: ThemeColorPicker = ["#FFF", "#000"]
view.themeBackgroundColor = colorPickers
Because themeBackgroundColor accepts an argument of type ThemeColorPicker,not Array. Nevertheless, "view.themeBackgroundColor = ["#FFF", "#000"]" does the same as initializing an instance of ThemeColorPicker by "Literal" and passing it to the themeBackgroundColor.
You may want to make your app download and install an indefinite number of themes. To fulfill this requirement, we provide plist mode. Simply put, you write configuration info such as colors, image cuts and so on, in a plist file. Then, you can use their keys in the logic code. So, the plist file and the resource files are used to constitute a theme package.
Usage demo of plist mode.
view.themeBackgroundColor = "Global.backgroundColor"
imageView.themeImage = "SelectedThemeCell.iconImage"
Similar with the index mode. Only the specific parameters become keys. And as such, we give it the extension ability.
The plist file name is the first paramter of the switching method. In this example, the plist file and other resource files are in the application bundle. It's also ok if they are in sandbox.
ThemeManager.setTheme(plistName: "Red", path: .MainBundle)
plist mode allow you install more themes without modifying logic code. So, you can add the feature that, downloading and installing themes for your app.
the screenshots of the plist and image files we used above:
Fully compatible with Objective-C, usage demo:
lbl.themeBackgroundColor = [ThemeColorPicker pickerWithColors:@[@"#FAF9F9", @"#E2E2E2"]];
- Written in Swift
- Fully compatible with Objective-C
- Based on runtime
- Simple integration
- Extension property prefix with "theme*", friendly with IDE auto-completion
- Support UIAppearance
- Index mode, fast integration
- Plist mode, extend infinite themes
- Friendly error logs
- Strongly typed ThemePicker, detect errors during compilling
- Complete demos
pod 'SwiftTheme'
use_frameworks!
github "jiecao-fm/SwiftTheme"
Copy all the files in "Source" folder into your project
Note:①
usage of index mode ②
usage of plist mode
SwiftTheme provides new properties for views, they all beigin with theme
. Such as themeBackgroundColor
corresponds backgroundColor
.
①
view.themeBackgroundColor = ThemeColorPicker(colors: "#FFF", "#000")
view.themeImage = ThemeImagePicker(names: "day", "night")
②
view.themeBackgroundColor = ThemeColorPicker(keyPath: "SomeColorKeyPath")
view.themeImage = ThemeImagePicker(keyPath: "SomeImageKeyPath")
Different type of properties receive different type of Pickers. Thus, IDE will warn you if you pass a wrong parameter.
When you switch themes, all the theme
properties you set will update with animation. Usage:
①
ThemeManager.setTheme(index: 0) // ThemePickers will use the first parameter, eg. "#FFF" "day"
ThemeManager.setTheme(index: 1) // ThemePickers will use the second parameter, eg. "#000" "night"
②
// use "day.plist" in the appllication bundle as the theme configuration file.
// In this mode, SwiftTheme will find the resource files in the appllication bundle.
ThemeManager.setTheme(plistName: "day", path: .MainBundle)
// use "night.plist" in the sandbox as the theme configuration file, "someURL" is its file path.
// In this mode, SwiftTheme will find the resource files in the same path.
ThemeManager.setTheme(plistName: "night", path: .Sandbox(someURL))
// use a dictionary as the theme configuration, but find resource files in the sandbox.(Not recommend)
ThemeManager.setTheme(dict: dict, path: .Sandbox(someURL))
SwiftTheme posts a notification named ThemeUpdateNotification
when theme changes, you can observe this notification anywhere and do whatever you want:
NotificationCenter.default.addObserver(
self,
selector: #selector(doSomethingMethod),
name: NSNotification.Name(rawValue: ThemeUpdateNotification),
object: nil
)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(doSomethingMethod) name:@"ThemeUpdateNotification" object:nil];
Child classes inherit the properties from their super class, such as UILabel have themealpha inherited from UIView. These properties will not be list in child classes below.
- var themeAlpha: ThemeCGFloatPicker?
- var themeBackgroundColor: ThemeColorPicker?
- var themeTintColor: ThemeColorPicker?
- func themeSetStatusBarStyle(picker: ThemeStatusBarStylePicker, animated: Bool)
- var themeTintColor: ThemeColorPicker?
- var themeFont: ThemeFontPicker?
- var themeTextColor: ThemeColorPicker?
- var themeHighlightedTextColor: ThemeColorPicker?
- var themeShadowColor: ThemeColorPicker?
- var themeBarStyle: ThemeBarStylePicker?
- var themeBarTintColor: ThemeColorPicker?
- var themeTitleTextAttributes: ThemeDictionaryPicker?
- var themeBarStyle: ThemeBarStylePicker?
- var themeBarTintColor: ThemeColorPicker?
- var themeSeparatorColor: ThemeColorPicker?
- var themeFont: ThemeFontPicker?
- var themeKeyboardAppearance: ThemeKeyboardAppearancePicker?
- var themeTextColor: ThemeColorPicker?
- var themeFont: ThemeFontPicker?
- var themeTextColor: ThemeColorPicker?
- var themeBarStyle: ThemeBarStylePicker?
- var themeBarTintColor: ThemeColorPicker?
- var themeOnTintColor: ThemeColorPicker?
- var themeThumbTintColor: ThemeColorPicker?
- var themeThumbTintColor: ThemeColorPicker?
- var themeMinimumTrackTintColor: ThemeColorPicker?
- var themeMaximumTrackTintColor: ThemeColorPicker?
- var themeBarStyle: ThemeBarStylePicker?
- var themeBarTintColor: ThemeColorPicker?
- var themeProgressTintColor: ThemeColorPicker?
- var themeTrackTintColor: ThemeColorPicker?
- var themePageIndicatorTintColor: ThemeColorPicker?
- var themeCurrentPageIndicatorTintColor: ThemeColorPicker?
- var themeImage: ThemeImagePicker?
- var themeActivityIndicatorViewStyle: ThemeActivityIndicatorViewStylePicker?
- func themeSetImage(picker: ThemeImagePicker, forState state: UIControlState)
- func themeSetBackgroundImage(picker: ThemeImagePicker, forState state: UIControlState)
- func themeSetTitleColor(picker: ThemeColorPicker, forState state: UIControlState)
- var themeBackgroundColor: ThemeCGColorPicker?
- var themeBorderWidth: ThemeCGFloatPicker?
- var themeBorderColor: ThemeCGColorPicker?
- var themeShadowColor: ThemeCGColorPicker?
// supported formats:
// "#ffcc00" RGB
// "#ffcc00dd" RGBA
// "#FFF" RGB in short
// "#013E" RGBA in short
①
ThemeColorPicker(colors: "#FFFFFF", "#000")
ThemeColorPicker.pickerWithColors(["#FFFFFF", "#000"])
②
ThemeColorPicker(keyPath: "someStringKeyPath")
ThemeColorPicker.pickerWithKeyPath("someStringKeyPath")
①
ThemeImagePicker(names: "image1", "image2")
ThemeImagePicker.pickerWithNames(["image1", "image2"])
ThemeImagePicker(images: UIImage(named: "image1")!, UIImage(named: "image2")!)
ThemeImagePicker.pickerWithImages([UIImage(named: "image1")!, UIImage(named: "image2")!])
②
ThemeImagePicker(keyPath: "someStringKeyPath")
ThemeImagePicker.pickerWithKeyPath("someStringKeyPath")
①
ThemeCGFloatPicker(floats: 1.0, 0.7)
ThemeCGFloatPicker.pickerWithFloats([1.0, 0.7])
②
ThemeCGFloatPicker(keyPath: "someNumberKeyPath")
ThemeCGFloatPicker.pickerWithKeyPath("someNumberKeyPath")
①
ThemeCGColorPicker(colors: "#FFFFFF", "#000")
ThemeCGColorPicker.pickerWithColors(["#FFFFFF", "#000"])
②
ThemeCGColorPicker(keyPath: "someStringKeyPath")
ThemeCGColorPicker.pickerWithKeyPath("someStringKeyPath")
①
ThemeFontPicker(fonts: UIFont.systemFont(ofSize: 10), UIFont.systemFont(ofSize: 11))
ThemeFontPicker.pickerWithFonts([UIFont.systemFont(ofSize: 10), UIFont.systemFont(ofSize: 11)])
②
// Reading font from plist is not supported now
①
ThemeDictionaryPicker(dicts: ["key": "value"], ["key": "value"])
ThemeDictionaryPicker.pickerWithDicts([["key": "value"], ["key": "value"]])
ThemeDictionaryPicker.pickerWithAttributes([NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)])
②
// Reading dictionary from plist is not supported now
①
ThemeBarStylePicker(styles: .default, .black)
ThemeBarStylePicker.pickerWithStyles([.default, .black])
ThemeBarStylePicker.pickerWithStringStyles(["default", "black"])
②
// name the key you like, but the available values are "default" and "black"
ThemeBarStylePicker(keyPath: "someStringKeyPath")
ThemeBarStylePicker.pickerWithKeyPath("someStringKeyPath")
①
ThemeStatusBarStylePicker(styles: .default, .lightContent)
ThemeStatusBarStylePicker.pickerWithStyles([.default, .lightContent])
ThemeStatusBarStylePicker.pickerWithStringStyles(["default", "lightContent"])
②
// name the key you like, but the available values are "default" and "lightContent"
ThemeStatusBarStylePicker(keyPath: "someStringKeyPath")
ThemeStatusBarStylePicker.pickerWithKeyPath("someStringKeyPath")
①
ThemeKeyboardAppearancePicker(styles: .default, .dark, .light)
ThemeKeyboardAppearancePicker.pickerWithStyles([.default, .dark, .light])
ThemeKeyboardAppearancePicker.pickerWithStringStyles(["default", "dark", "light"])
②
// name the key you like, but the available values are "default", "dark" and "light"
ThemeKeyboardAppearancePicker(keyPath: "someStringKeyPath")
ThemeKeyboardAppearancePicker.pickerWithKeyPath("someStringKeyPath")
①
ThemeActivityIndicatorViewStylePicker(styles: .whiteLarge, .white, .gray)
ThemeActivityIndicatorViewStylePicker.pickerWithStyles([.whiteLarge, .white, .gray])
ThemeActivityIndicatorViewStylePicker.pickerWithStringStyles(["whiteLarge", "white", "gray"])
②
// name the key you like, but the available values are "whiteLarge", "white" and "gray"
ThemeActivityIndicatorViewStylePicker(keyPath: "someStringKeyPath")
ThemeActivityIndicatorViewStylePicker.pickerWithKeyPath("someStringKeyPath")
Download this project and find more. There are two demo targets:
Demo
shows how to use index mode and how to save the last selection of themes and other general usages.PlistDemo
shows how to use plist mode and how to download themes that packaged in zip files.
-
Why doesn't themeSetStatusBarStyle work as expected?
In your app's
Info.plist
you will need to setView Controller-based status bar appearence
toNO
. -
Can I manually cancel the theme of a property?
Sure, just make it
nil
—example:view.themeBackgroundColor = nil
.
If you find a bug or need a help, you can create a issue
We are happy to accept pull requests :D. But please make sure it's needed by most developers and make it simple to use. If you are not sure, create an issue and we can discuss it before you get to coding.
The MIT License (MIT)
Copyright (c) 2016 节操精选 http://jiecao.fm
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.