A dark theme framework for iOS, quickly and easily adapting to dark mode.
Mainland China users can accessThis link
中文介绍
github does not display picture solutions
Configure dark resources:
Create + (NSDictionary<id, id> *)llDarkTheme
class method in any NSObject category of the project (it is recommended to create a separate category). The key of the dictionary represents the color/picture name/picture address under the light theme, and the dictionary’s Value represents the color/picture name/picture address under the dark theme. You can refer to the sample code:
+ (NSDictionary<id, id> *)llDarkTheme {
return @{
UIColor.whiteColor : kColorRGB(27, 27, 27),
kColorRGB(240, 238, 245) : kColorRGB(39, 39, 39),
[UIColor colorWithRed:14.0 / 255.0 green:255.0 / 255.0 blue:0.0 alpha:1.0] : [UIColor colorWithRed:0.0 green:14.0 / 255.0 blue:255.0 / 255.0 alpha:1.0],
@"background_light" : @"background_dark",
@"~/path/background_light.png" : @"~/path/background_dark.png",
};
}
Tips:
1.It is not necessary to fill in all colors/pictures in all cases. For dark colors that are occasionally or rarely used, you can refer to the advanced usage to adapt them separately. 2.if the picture path is filled in, the complete picture path (including the suffix) must be filled in.
UIColor and CGColor only need to append .themeColor(nil). UIImage only needs to replace imageNamed or imageWithContentsOfFile with themeImage.
// UIColor
UIColor.redColor; // Previous usage
UIColor.redColor.themeColor(nil); // Current usage
// CGColor
UIColor.redColor.CGColor; // Previous usage
UIColor.redColor.themeCGColor(nil); // Current usage
// UIImage
[UIImage imageNamed:@"lightImageName"]; // Previous usage
[UIImage themeImage:@"lightImageName"]; // Current usage
Tips:
1.themeImage is adapted to two methods, imageNamed and imageWithContentsOfFile, which can pass the image name or the image path. 2.Only the adapted Color and Image will be refreshed when the theme is switched.
1. If the parameter in themeColor() is a specific Color object, the dark theme will refresh with the specified Color object.
If it is nil, it will return to the dark color refresh configured in llDarkTheme,
If llDarkTheme is not configured, it will return the color under the light theme.
2. The function of the themeCGColor() parameter is the same as the function of the themeColor() parameter.
3. themeImage() has 2 parameters, the parameter can be the image name or the image address,
The first parameter represents the picture used under the light theme (required),
The second parameter represents the picture used under the dark theme (can be empty),
If the second parameter is empty, the treatment is the same as if themeColor() is empty.
4. appearanceBindUpdater,All objects inherited from UIView have this property,
It will be called when the object needs to be refreshed, and you can implement your own refresh logic here.
It is only called when a refresh is needed, and the theme change does not necessarily require refreshing the UI.
5. userInterfaceStyle,Similar to the overrideUserInterfaceStyle method of iOS13 system,
But the function is more powerful than overrideUserInterfaceStyle,
It supports all objects, such as CALayer.
It supports system usage below iOS13.
6. themeDidChange,All objects have this property, which is the same as ThemeDidChangeNotification.
themeDidChange will be released when the object is released,
Can be used in multiple places, the callback order is not guaranteed,
Unlike appearanceBindUpdater, themeDidChange is called whenever the theme changes.
7. systemThemeDidChange,All objects have this property, which is the same as SystemThemeDidChangeNotification.
The release timing is the same as themeDidChange,
Can be used in multiple places, the callback order is not guaranteed,
As long as the system theme changes, systemThemeDidChange will be called.
8. darkStyle,All UIImageView objects have this method to adapt to image objects without dark images, such as web images.
DarkStyle has 3 parameters. The first parameter determines how to adapt to the dark theme. There are currently two types: LLDarkStyleSaturation and LLDarkStyleMask.
LLDarkStyleMask uses mask adaptation, and LLDarkStyleSaturation adapts by reducing the saturation of the original image.
The second parameter determines the transparency/saturation value of the mask. For specific usage, please refer to the source code comments.
The third parameter can be nil. When using LLDarkStyleSaturation, you need to pass a unique string as an identifier, usually the url of the image.
Sample code:
UIImageView *imageView = [[UIImageView alloc] init];
NSString *url = @"Picture URL";
imageView.darkStyle(LLDarkStyleSaturation, 0.2, url);
// imageView.darkStyle(LLDarkStyleMask, 0.5, nil);
9. updateDarkTheme:,If you need to modify the dark theme configuration information at runtime, or need to obtain dark theme configuration information from the Internet, you can use updateDarkTheme: to achieve the goal.
Please ensure that the dark theme information is configured before the first UI object is loaded, otherwise it will be invalid.
Sample code:
NSDictionary *darkTheme = @{
UIColor.whiteColor : kColorRGB(27, 27, 27),
kColorRGB(240, 238, 245) : kColorRGB(39, 39, 39),
[UIColor colorWithRed:14.0 / 255.0 green:255.0 / 255.0 blue:0.0 alpha:1.0] : [UIColor colorWithRed:0.0 green:14.0 / 255.0 blue:255.0 / 255.0 alpha:1.0],
@"background_light" : @"background_dark",
@"~/path/background_light.png" : @"~/path/background_dark.png",
};
[LLDarkSource updateDarkTheme:darkTheme];
10. thirdControlClassName,If you need to support the refresh method of the third-party control, you can implement the refresh logic separately in appearanceBindUpdater, or you can implement the refresh logic according to the following methods, and the following methods are more recommended.
First, you need to implement the thirdControlClassName class method and return an array containing the class name string of the third party control.
Then implement the refresh+class name string object method, and implement the refresh logic of the third-party control in the method. You can refer to the YYLabel refresh logic that has been implemented in the LLThird.m file.
For details, you can download the project and view the Demo to understand the specific implementation.
11. If you need to support the adaptation of the dark startup image in iOS13 or lower, please name the dark image according to the specified rules and place it in any directory of the project.
Naming rule: launchImage_<screen width>_<screen height>.
For example: launchImage_414_736, this dark launch image will appear after switching to dark mode on models with a screen width of 414×736 and systems below iOS13.
If you want to adapt the horizontal image, you only need to exchange the width and height positions, for example: launchImage_736_414.
The specific effect can be viewed by running Demo on the system below iOS13 and switching to dark mode.
For specific naming, please refer to the image naming method under the LaunchImage folder in the Demo project. It contains the dark launch image of all models below iOS 13 (including horizontal screen, excluding models before iPhone 6).
12. LLLaunchScreen provides some class methods, reasonable use of these class methods can perfectly replace any startup image of APP, including "dark vertical launch Image", "dark horizontal launch Image", "light vertical launch Image", "Light horizontal launch Image".
Please refer to the LLLaunchScreen.h file for specific methods.
For usage method, please refer to Demo.
The sample diagram of darkStyle method No. 8 in advanced usage (in order to highlight the effect, the saturation and transparency are adjusted very low):
It only takes 3 steps to quickly and perfectly adapt to the dark theme mode. After testing, most of the projects can be adapted within 0.5 days.
Prerequisites
, or refer to Advanced Method 9
to obtain resource adaptation from the network.Basic Usage
and Advanced Usage
.Tips:
WKWebView
, you can click the linkRefer to the article for adaptation.Advanced Usage 11
and Advanced Usage 12
.github "internetWei/LLDark"
to your Cartfile.carthage update --platform ios
and add the framework to your project.The project supports iOS 9.0 and Xcode 10.0 at least. If you want to use it on lower systems, please contact the author.
Assets.xcassets
. The test found that the images in NSAttributedString will not be refreshed when the theme is switched (loaded with imageNamed:
). This is a system bug. The solution is to put the image under the project and load it with themeImage:
.imageWithRenderingMode:
method。Calling this method will regenerate and return a UIImage instance object,This will cause the normal refresh。Please use renderingModeFrom
method instead。If you have better improvements, please pull reqeust me
If you have any better comments, please create oneIssue
The author can be contacted by this emailinternetwei@foxmail.com
LLDark is released under the MIT license. See LICENSE file for details.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。