If you want to implement night mode in Swift project without import Objective-C code. NightNight
is the Swift version which does the same work.
How To Get Started
DKNightVersion supports multiple methods for installing the library in a project.
Installation with CocoaPods
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 the Get Started section for more details.
Podfile
To integrate DKNightVersion into your Xcode project using CocoaPods, specify it in your Podfile:
pod "DKNightVersion"
Then, run the following command:
$ pod install
Import
Import DKNightVersion header file
#import <DKNightVersion/DKNightVersion.h>
Usage
Checkout DKColorTable.txt file in your project, which locates in Pods/DKNightVersion/Resources/DKNightVersion.txt.
NORMAL NIGHT
#ffffff #343434 BG
#aaaaaa #313131 SEP
You can also create another colour table file, and specify it with DKColorTable.
A, set color picker like this with DKColorPickerWithKey, which generates a DKColorPicker block
Modify themeVersion 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";
Post Notification
Every time the current theme version changes, DKNightVersionManager would post a DKNightVersionThemeChangingNotification. If you want to do some customisation, you can observe this notification and react with proper actions.
DKColorPicker
DKColorPicker is the core of DKNightVersion. And this lib adds dk_colorPicker to every UIKit and Core Animation components. Ex:
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 called DKColorTable.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 and NIGHT 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 this DKColorTable.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 current themeVersion (ex: NORMALNIGHT and RED) and key (ex: BG, SEP) to find the corresponding colour in DKColorTable.
DKColorTable has a property file, it will loads the color setting in this file when + [DKColorTable sharedColorTable is called. Default value of file is DKColorTable.txt.
@property (nonatomic, strong) NSString *file;
You can also add another file into your project and fill your colour setting in that file.
// color.txt
NORMAL NIGHT
#ffffff #343434 BG
Also, do not forget to change the file property of the colour table.
DKColorPickerWithRGB will return a DKColorPicker which set background color to #ffffff when current theme version is DKThemeVersionNormal and #343434 when it is DKThemeVersionNight.
There are also some similar functions like DKColorPickerWithColors
The first argument passed into the function is used for NORMAL theme, and the second is used for NIGHT theme, the themes order is determined by the configuration in
DKColorTable.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
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 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.
Demo
How To Get Started
DKNightVersion supports multiple methods for installing the library in a project.
Installation with CocoaPods
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 the Get Started section for more details.
Podfile
To integrate DKNightVersion into your Xcode project using CocoaPods, specify it in your
Podfile
:Then, run the following command:
Import
Import DKNightVersion header file
Usage
Checkout
DKColorTable.txt
file in your project, which locates inPods/DKNightVersion/Resources/DKNightVersion.txt
.A, set color picker like this with
DKColorPickerWithKey
, which generates a DKColorPicker blockAfter the current theme version change to
DKThemeVersionNight
, the view background colour would switch to#343434
.Alternatively, you could change the theme version by manager’s property
themeVersion
which is a stringAdvanced Usage
There are two approaches you can use to integrate night mode to your iOS App.
DKNightVersionManager
The latest version for DKNightVersion add a readonly
dk_manager
property forNSObject
returns theDKNightVersionManager
singleton.Change Theme
You can call
nightFalling
ordawnComing
to switch the current theme version toDKThemeVersionNight
orDKThemeVersionNormal
.Modify
themeVersion
property to switch the theme version directly.Post Notification
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
DKColorPicker
is the core of DKNightVersion. And this lib adds dk_colorPicker to every UIKit and Core Animation components. Ex:DKColorPicker is defined in
DKColor.h
file receives aDKThemeVersion
as the parameter and returns aUIColor
.Use
DKColorPickerWithKey(key)
to obtainDKColorPicker
fromDKColorTable
Use
DKColorPickerWithRGB
to generate aDKColorPicker
DKColorTable
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 called
DKColorTable.txt
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
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 this
DKColorTable.txt
file as many as you want.The last column is the key for a colour entry, DKNightVersion uses the current
themeVersion
(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
.You can also add another file into your project and fill your colour setting in that file.
Also, do not forget to change the
file
property of the colour table.The code above would reload colour setting from
color.txt
file.Create temporary DKColorPicker
If you’d want to create some temporary DKColorPicker, you can use these methods.
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 like
DKColorPickerWithColors
DKColor
also provides a cluster of convenientAPI
which returnsDKColorPicker
block, these blocks return the same colour in different themes.pickerify
DKNightVersion provides a powerful feature which can generate dk_xxxColorPicker with a macro called
pickerify
.It automatically generates
dk_cellTintColorPicker
for you.DKImagePicker
Use
DKImagePicker
to change images whenmanager.themeVersion
changes.The first argument passed into the function is used for
NORMAL
theme, and the second is used forNIGHT
theme, the themes order is determined by the configuration in DKColorTable.txt file which is NORMAL and NIGHT.If your file like this:
Set your image picker in this order:
The order of images or names is the same in DKColorTable.txt file.
Contribute
Feel free to open an issue or pull request, if you need help or there is a bug.
Contact
Todo
License
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 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.