- Notifications
You must be signed in to change notification settings - Fork19
👕👚 Theme management in Swift
License
onmyway133/EasyTheme
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Ever want to support Night mode? Or skin the app differently depending on the seasons? Or toggle features according to paid status? Well, those are actually reactions to app events.
Many other frameworks encourage you to use hard coded values, likelabel.xyz_textColors = [.red, .blue], textField.xyz_fonts = [font1, font2], ...
. This also makes it very hard to change because the usage of index, you need to remember that the 1st index is this theme, the 2nd index is that theme, ... Also,xyz_textColors
is like trying to replicate the entireUIKit
APIs, which updates often 😱
Themes is here to help. Usually, you have a finite number of colors and fonts in an app. You can have many more but that is not encourage and has design smells. When you have a theme, changing happens in one place.
- Universal support for iOS, macOS, tvOS, watchOS
- Complete control over themes
- Update existing views
- Protocol oriented
- Extensible
Declare your theme by conforming toTheme
, which is just a marker protocol. You can declare whatever you like, including nested objects, all depending on your need. You can also create as many themes as you like
structMyTheme:Theme{lettopImage:UIImageletcellColor:UIColorletbackgroundColor:UIColorletname:StringlettitleFont:UIFontletsubtitleFont:UIFont}
Then create some themes based on your templates
letdayTheme=MyTheme(topImage:UIImage(named:"day"), cellColor:.white)letnightTheme=MyTheme(topImage:UIImage(named:"night"), cellColor:.black)
The beauty of this is that you caninit
your theme from json, which can be fetched from backend 🚀
letjson=["primary_color":"#21ABE9","font_name":"Chewy"]letunicornTheme=MyTheme(json)
When app launches, you need to declare 1 theme as the current, it can be loaded from cache
ThemeManager.shared.currentTheme= dayTheme
You can do this wherever you like. It is set using the current theme, and whenever theme changes
// ViewController.swiftoverridefunc viewDidLoad(){ super.viewDidLoad()use(MyTheme.self){ $0.title= $1.name $0.tableView.backgroundColor= $1.backgroundColor $0.navigationController?.navigationBar.setBackgroundImage($1.topImage, for:.default) $0.tableView.rowHeight= $1.name=="Unicorn"?180:120 $0.tableView.reloadData()}}// Cell.swiftoverridefunc awakeFromNib(){ super.awakeFromNib() imageView.layer.cornerRadius=5 imageView.layer.masksToBounds=trueuse(MyTheme.self){ $0.titleLabel.font= $1.titleFont $0.subtitleLabel.font= $1.subtitleFont $0.container.backgroundColor= $1.cellColor}}
Change the current theme is as easy as assigning a new theme. All happens in real time and very fast
ThemeManager.shared.currentTheme= nightTheme
Themes is available throughCocoaPods. To installit, simply add the following line to your Podfile:
pod'Themes'
Themes is also available throughCarthage.To install just write into your Cartfile:
github"onmyway133/Themes"
Themes can also be installed manually. Just download and dropSources
folders in your project.
Khoa Pham,onmyway133@gmail.com
We would love you to contribute toThemes, check theCONTRIBUTING file for more info.
Themes is available under the MIT license. See theLICENSE file for more info.
About
👕👚 Theme management in Swift