# BWThemeKit **Repository Path**: flytoo/BWThemeKit ## Basic Information - **Project Name**: BWThemeKit - **Description**: No description available - **Primary Language**: Swift - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-04-04 - **Last Updated**: 2023-02-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # BWThemeKit [![CI Status](https://img.shields.io/travis/朱旭宏/BWThemeKit.svg?style=flat)](https://travis-ci.org/朱旭宏/BWThemeKit) [![Version](https://img.shields.io/cocoapods/v/BWThemeKit.svg?style=flat)](https://cocoapods.org/pods/BWThemeKit) [![License](https://img.shields.io/cocoapods/l/BWThemeKit.svg?style=flat)](https://cocoapods.org/pods/BWThemeKit) [![Platform](https://img.shields.io/cocoapods/p/BWThemeKit.svg?style=flat)](https://cocoapods.org/pods/BWThemeKit) ## Example To run the example project, clone the repo, and run `pod install` from the Example directory first. ## Requirements ``` iOS 9+ ``` ## Installation BWThemeKit is available through [CocoaPods](https://cocoapods.org). To install it, simply add the following line to your Podfile: ```ruby pod 'BWThemeKit' ``` ## Configuration 1. Add `New Run Script Phase` and `renamed` to `BWThemeKit` 2. Add below shell scripts codes: ```shell xcrun --sdk macosx swiftc -parse-as-library $SCRIPT_INPUT_FILE_0 -o .bwthemer ./.bwthemer ``` 3. Add `Input Files` with belows: ```shell $(PODS_ROOT)/BWThemeKit/Generator.swift ``` 4. Build your xcode project. 5. Drag the generated `Theme` folder files to your xcode project with `Create groups` & `Added to targets`: ```shell /your_project_dir/Theme ├── Colors │   ├── Dark.json │   └── Light.json └── Configure.json ``` 6. Edit the `Configure.json` with your custom `Theme-Enum-Names`(defaults are `DemoTheme`、`DemoThemeColor`), suggested: `AppTheme`、`AppThemeColor` or `BWTheme`、`BWThemeColor`(your-app-name-words-abbr): ```json { "name": "AppTheme", "styles": [ "Light", "Dark" ], "colors": { "name": "AppThemeColor", "files": [ "Light", "Dark" ] } } ``` ​ Also configure with theme style names with `Light, Dark` and theme color json files with `Light, Dark` what you can edit them but must be same as files under folder `Colors`. ``` ├── Colors │   ├── Dark.json │   └── Light.json ``` 6. Build xcode project again. 7. Drag the generated `Generates` folder files to your project with `Create groups` & `Added to targets`: ``` /your_project_dir/Theme └── Generates ├── AppTheme.swift ├── AppThemeColor.swift ├── AppThemeColors.json ├── UIView+Themeable.swift └── UIViewController+Themeable.swift ``` ​ And now whatever you updated the contents of file `Colors/Light.json` , when after the project rebuild, the above swift files with be updated immediately. 8. Configure the theme updating codes in your `xib/view/view controller`: ```swift class ViewController: UIViewController, { override func viewDidLoad() { super.viewDidLoad() /// Update theme style manually when view is did load. /// Or if you settled up the extension `UIViewController+Themeable` in `app did finish launching`, /// it will automatically handle this when `viewDidLoad` updateThemeStyleIfNeeded() } } extension ViewController: AppThemeAdaptableResponder{ func updateThemeIfNeeded(style: AppThemeStyle) { view.backgroundColor = .use(AppThemeColor.Common.red) } } ``` - Settle up theme extensions: `UIView+Themeable`、`UIViewController+Themeable`: ```swift func application( _ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? ) -> Bool { // Set the default initial theme style or it will be `unknown` if AppTheme.shared.currentStyle == .unknown { AppTheme.shared.updateStyle(with: .light) } // Settle up theme runtime things UIView.themeableSetups() UIViewController.themeableSetups() return true } ``` - Set theme color in xib: - `TColor Key`: the themeable color key equals to the view's key/key path: ```swift backgroundColor、tintColor、borderColor ... ``` - `TColor Value`: the theme color value for your theme json files configured: ``` common.red、view.background、button.tint ``` ​ Likes `Light.json` color file configured: ```json [ { "name": "Common", "colors": [ { "name": "red", "rgba": "255, 0, 0, 1" } ] },{ "name": "View", "colors": [ { "name": "background", "rgba": "255, 0, 0, 1" } ] },{ "name": "Button", "colors": [ { "name": "tint", "rgba": "255, 0, 0, 1" } ] } ] ``` > We strongly suggest that you should define and configure your customized theme colors with `Component Based Semantic Color`,what is simple say that you should make your views `component-based` and name your colors `semantic`,For example: `Button` component with semantic colors: `background、tint、title、title2、title3、border、shadow ...` and `View` component may has same named colors but they two totally different. This will make your theme colors unique and flexible and easy to update, when you updated the one of component's colors it will not affect the others components' theme colors even though they have a same name likes `background`、`tint`。 - Update theme in custom view: ```swift class ThemeableView: UIView { override init(frame: CGRect) { super.init(frame: frame) updateThemeStyleIfNeeded() } required init?(coder: NSCoder) { super.init(coder: coder) updateThemeStyleIfNeeded() } } extension ThemeableView: AppThemeAdaptableResponder{ func updateThemeIfNeeded(style: AppThemeStyle) { backgroundColor = .use(AppThemeColor.Common.red) } } ``` ## Author Flytoo, 1906457616@qq.com ## License BWThemeKit is available under the MIT license. See the LICENSE file for more info.