- Notifications
You must be signed in to change notification settings - Fork515
Manage Colors, Integrate Night/Multiple Themes. (Unmaintained)
License
draveness/DKNightVersion
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- Easily integrate and high performance
- Providing UIKit and CoreAnimation category
- Read colour customisation from file
- Support different themes
- Generate picker for other libs with one line macro
If you want to implement night mode in Swift project without import Objective-C code.NightNightis the Swift version which does the same work.
DKNightVersion supports multiple methods for installing the library in a project.
CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like DKNightVersion in your projects. See theGet Started section for more details.
To integrate DKNightVersion into your Xcode project using CocoaPods, specify it in yourPodfile
:
pod"DKNightVersion"
Then, run the following command:
$ pod install
Import DKNightVersion header file
#import<DKNightVersion/DKNightVersion.h>
CheckoutDKColorTable.txt
file in your project, which locates inPods/DKNightVersion/Resources/DKNightVersion.txt
.
NORMAL NIGHT#ffffff #343434 BG#aaaaaa #313131 SEP
You can also create another colour table file, and specify it withDKColorTable.
A, set color picker like this withDKColorPickerWithKey
, which generates a DKColorPicker block
self.view.dk_backgroundColorPicker = DKColorPickerWithKey(BG);
After the current theme version change toDKThemeVersionNight
, the view background colour would switch to#343434
.
[DKNightVersionManagernightFalling];
Alternatively, you could change the theme version by manager's propertythemeVersion
which is a string
DKNightVersionManager *manager = [DKNightVersionManagersharedInstance];manager.themeVersion = DKThemeVersionNormal;
There are two approaches you can use to integrate night mode to your iOS App.
The latest version for DKNightVersion add a readonlydk_manager
property forNSObject
returns theDKNightVersionManager
singleton.
You can callnightFalling
ordawnComing
to switch the current theme version toDKThemeVersionNight
orDKThemeVersionNormal
.
[self.dk_managerdawnComing];[self.dk_managernightFalling];
ModifythemeVersion
property to switch the theme version directly.
self.dk_manager.themeVersion = DKThemeVersionNormal;self.dk_manager.themeVersion = DKThemeVersionNight;// if there is a RED column in DKColorTable.txt (default) or in// other `file` if you customize `file` property for `DKColorTable`self.dk_manager.themeVersion =@"RED";
Every time the current theme version changes,DKNightVersionManager
would post aDKNightVersionThemeChangingNotification
. If you want to do some customisation, you can observe this notification and react with proper actions.
DKColorPicker
is the core of DKNightVersion. And this lib adds dk_colorPicker to every UIKit and Core Animation components. Ex:
@property (nonatomic, copy, setter = dk_setBackgroundColorPicker:) DKColorPicker dk_backgroundColorPicker;@property (nonatomic, copy, setter = dk_setTintColorPicker:) DKColorPicker dk_tintColorPicker;
DKColorPicker is defined inDKColor.h
file receives aDKThemeVersion
as the parameter and returns aUIColor
.
typedef UIColor *(^DKColorPicker)(DKThemeVersion *themeVersion);
Use
DKColorPickerWithKey(key)
to obtainDKColorPicker
fromDKColorTable
view.dk_backgroundColorPicker = DKColorPickerWithKey(BG);
Use
DKColorPickerWithRGB
to generate aDKColorPicker
view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0x343434);
DKColorTable
is a new feature in DKNightVersion which providing us with an elegant way to manage colour setting in a project. Use as follows:
There is a file calledDKColorTable.txt
NORMAL NIGHT#ffffff #343434 BG#aaaaaa #313131 SEP
The first line of this file indicated different themes.NORMAL is required column, and others are optional. So if you don't need to integrate different themes in your app, leave the first column in this file, like this:
NORMAL#ffffff BG#aaaaaa SEP
NORMAL
andNIGHT
are two different themes,NORMAL
is the default and for normal mode.NIGHT
is optional and for night mode.
You can add multiple columns in thisDKColorTable.txt
file as many as you want.
NORMAL NIGHT RED#ffffff #343434 #ff0000 BG#aaaaaa #313131 #ff0000 SEP
The last column is the key for a colour entry, DKNightVersion uses the currentthemeVersion
(ex:NORMAL
NIGHT
andRED
) and key (ex:BG
,SEP
) to find the corresponding colour in DKColorTable.
DKColorTable
has a propertyfile
, it will loads the color setting in thisfile
when+ [DKColorTable sharedColorTable
is called. Default value offile
isDKColorTable.txt
.
@property (nonatomic, strong)NSString *file;
You can also add another file into your project and fill your colour setting in that file.
// color.txtNORMAL NIGHT#ffffff #343434 BG
Also, do not forget to change thefile
property of the colour table.
[DKColorTablesharedColorTable].file =@"color.txt"
The code above would reload colour setting fromcolor.txt
file.
If you'd want to create some temporary DKColorPicker, you can use these methods.
view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff,0x343434);
DKColorPickerWithRGB
will return a DKColorPicker which set background color to#ffffff
when current theme version isDKThemeVersionNormal
and#343434
when it isDKThemeVersionNight
.
There are also some similar functions likeDKColorPickerWithColors
DKColorPickerDKColorPickerWithRGB(NSUIntegernormal, ...);DKColorPickerDKColorPickerWithColors(UIColor *normalColor, ...);
DKColor
also provides a cluster of convenientAPI
which returnsDKColorPicker
block, these blocksreturn the same colour in different themes.
+ (DKColorPicker)colorPickerWithUIColor:(UIColor *)color;+ (DKColorPicker)colorPickerWithWhite:(CGFloat)white alpha:(CGFloat)alpha;+ (DKColorPicker)colorPickerWithHue:(CGFloat)hue saturation:(CGFloat)saturation brightness:(CGFloat)brightness alpha:(CGFloat)alpha;+ (DKColorPicker)colorPickerWithRed:(CGFloat)red green:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha;+ (DKColorPicker)colorPickerWithCGColor:(CGColorRef)cgColor;+ (DKColorPicker)colorPickerWithPatternImage:(UIImage *)image;#if __has_include(<CoreImage/CoreImage.h>)+ (DKColorPicker)colorPickerWithCIColor:(CIColor *)ciColorNS_AVAILABLE_IOS(5_0);#endif+ (DKColorPicker)blackColor;+ (DKColorPicker)darkGrayColor;+ (DKColorPicker)lightGrayColor;+ (DKColorPicker)whiteColor;+ (DKColorPicker)grayColor;+ (DKColorPicker)redColor;+ (DKColorPicker)greenColor;+ (DKColorPicker)blueColor;+ (DKColorPicker)cyanColor;+ (DKColorPicker)yellowColor;+ (DKColorPicker)magentaColor;+ (DKColorPicker)orangeColor;+ (DKColorPicker)purpleColor;+ (DKColorPicker)brownColor;+ (DKColorPicker)clearColor;
DKNightVersion provides a powerful feature which can generate dk_xxxColorPicker with a macro calledpickerify
.
@pickerify(TableViewCell, cellTintColor)
It automatically generatesdk_cellTintColorPicker
for you.
UseDKImagePicker
to change images whenmanager.themeVersion
changes.
imageView.dk_imagePicker = DKImagePickerWithNames(@"normal",@"night");
The first argument passed into the function is used forNORMAL
theme, and the second is used forNIGHT
theme, the themes order is determined by the configuration inDKColorTable.txt file which is NORMAL and NIGHT.
If your file like this:
NORMAL NIGHT RED#ffffff #343434 #fafafa BG#aaaaaa #313131 #aaaaaa SEP#0000ff #ffffff #fa0000 TINT#000000 #ffffff #000000 TEXT#ffffff #444444 #ffffff BAR
Set your image picker in this order:
imageView.dk_imagePicker = DKImagePickerWithNames(@"normal",@"night",@"red");
The order of images or names is the same in DKColorTable.txt file.
DKImagePickerDKImagePickerWithImages(UIImage *normalImage, ...);DKImagePickerDKImagePickerWithNames(NSString *normalName, ...);
Feel free to open an issue or pull request, if you need help or there is a bug.
- Documentation
DKNightVersion is available under the MIT license. See the LICENSE file for more info.
The MIT License (MIT)
Copyright (c) 2015 Draveness
Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in allcopies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, 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 THESOFTWARE.
About
Manage Colors, Integrate Night/Multiple Themes. (Unmaintained)