LLDark

Powerful dark theme framework for iOS that quickly ADAPTS to dark mode. Domestic users can visit this link

features

  • Integration is simple and requires only a few code changes to fit perfectly.
  • High performance. The specified page is updated only when the page needs to be updated. Related cache policies shorten the refresh duration.
  • Powerful, all use UIColor, UIImage, CGColor can be perfectly adapted.
  • Compatible with iOS13 and later models.
  • Supports fetching dark theme configurations from the network.
  • Auto-adaptation startup picture shows the current theme mode of the APP.

Demo

usage

The premise

Configure dark resources: Create the + (NSDictionary

*)llDarkTheme class method in any NSObject class (it is recommended to create a separate topic class). The key of the dictionary represents the color/image name/image address under the light theme. The value of the dictionary represents the color/picture name/picture address under the dark theme. For example 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"}; }Copy the code

Tips: 1. You do not need to include all the colors/images for all situations. For occasional or rare dark colors, refer to advanced usage separately. 2. The image name does not need to consider the multigraph relationship; If you fill in the picture path, be sure to fill in the complete picture path (including the suffix).

Basic usage

UIColor and CGColor just append.themecolor (nil). UIImage simply replaces imageNamed or imageWithContentsOfFile with themeImage.

// UIColor
UIColor.redColor; // The previous usage
UIColor.redColor.themeColor(nil); // Present usage

// CGColor
UIColor.redColor.CGColor; // The previous usage
UIColor.redColor.themeCGColor(nil); // Present usage

// UIImage
[UIImage imageNamed:@"lightImageName"]; // The previous usage
[UIImage themeImage:@"lightImageName"]; // Present usage
Copy the code

Tips: 1. ThemeImage adaptation imageNamed and imageWithContentsOfFile two methods, can transfer images name, also can pass image path. 2. Only the appropriate Color and Image will be refreshed when the theme is switched.

Advanced usage

1.If the argument in themeColor() is a specific Color object, the dark theme will be refreshed with the specified Color object, if sonilIf llDarkTheme is not configured, the color under light theme will be returned.2.The themeCGColor() parameter does the same thing as the themeColor() parameter.3.ThemeImage ()2The parameter can be the picture name, also can be the picture address, the first1This parameter represents the image to be used under the light theme (required), and no2This parameter represents the image used under the dark theme (can be empty), and the first2An empty argument to themeColor() is treated the same way.4.AppearanceBindUpdater, all inherited fromUIViewThis property is called when the object needs to be refreshed. You can implement your own refresh logic here. Called only when a refresh is required; theme changes do not necessarily require a UI refresh.5.UserInterfaceStyle, similar iOS13 overrideUserInterfaceStyle method in the system, but the function is more powerful than overrideUserInterfaceStyle, it supports all the object, for exampleCALayer. It supports iOS13 and up.6.ThemeDidChange, all objects have this property, function and ThemeDidChangeNotification, themeDidChange will be released in the object is released, can be used in multiple places, is no guarantee that the callback order, Unlike appearanceBindUpdater, themeDidChange is called whenever the theme changes.7.SystemThemeDidChange, all objects have this property, function and SystemThemeDidChangeNotification, release time and themeDidChange, can be used in multiple places, is no guarantee that the callback order, SystemThemeDidChange is called whenever the system theme changes.8.DarkStyle, allUIImageViewObjects have this method for image objects that do not have dark images, such as web images. DarkStyle have3The first parameter1At present, there are LLDarkStyleSaturation and LLDarkStyleMask. LLDarkStyleMask uses mask adaptation, and LLDarkStyleSaturation ADAPTS by reducing the original image's satiety. The first2Parameters determine the mask transparency/satiety value, the specific use can see the source comment. The first3Can benilLLDarkStyleSaturation requires passing a unique string as the identifier, usually the URL of the image. Example code:UIImageView *imageView = [[UIImageView alloc] init];
NSString *url = @ "image URL";
imageView.darkStyle(LLDarkStyleSaturation, 0.2, url);
/ / imageView. DarkStyle (LLDarkStyleMask, 0.5, nil);

9.UpdateDarkTheme: if you need to modify the dark theme configuration at runtime, or if you need to obtain the dark theme configuration from the network, you can use updateDarkTheme: to do this. Please make sure that on the1Dark theme information is configured before loading UI objects, otherwise it will be invalid. Example 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 required to support the first3Refresh logic can be implemented separately in appearanceBindUpdater, or in accordance with the following method. The following method is recommended. First you need to implement the thirdControlClassName class method and return an array containing the first3Class name string for square control. Then implement the refresh+ class name string object method, and implement the th in the method3For the refresh logic of the square control, you can refer to the refresh logic of YYLabel that has been implemented in the llThird. m file. For details, you can download the project and view Demo.Copy the code

Here’s an example of the darkStyle method from Advanced Use # 8 (the satiety and opacity are purposely low for the effect) :

Rapid adaptation

It takes only 3 steps to quickly and perfectly adapt to the dark theme mode. After testing, most projects can be adapted within 0.5 days, and a few projects can be adapted within 1 day, rarely requiring more than 1 day to adapt.

  1. Configure dark theme resources, referenceThe premise, can also refer toAdvanced Method 9Obtain resource adaptation from the network.
  2. Adapt the Color and Image to the theme Color and theme Image. Refer to the adaptation methodBasic usageandAdvanced usage.
  3. Run the project and check for integrity.

Tips: If you also need to adapt WKWebView, you can click the link to refer to the article to do so.

The installation

CocoaPods

  1. Update cocoapods to the latest version.
  2. Add pod ‘LLDark’ to your Podfile.
  3. Perform pod install or POD Update.
  4. Import < LLDark/LLDark. H >.

Manual installation

  1. Download everything in the LLDark folder.
  2. Add (drag and drop) the LLDark folder from the LLDark project to your project.
  3. Import LLDark. H.

System requirements

The minimum support for this project is iOS9.0 and Xcode10.0, please contact the author if you want to use it on lower systems.

Pay attention to the point

  1. You need to listen to the theme mode to change the status bar color.

Known issues

  • Please do not place the image resources that need to be adapted to dark themes in assets. xcassets, otherwise it may not be obtained.
  • Other theme modes are not supported for the time being, and a variety of themes will be supported later.

Contact the author

If you have better improvements, please pull me back

If you have any better ideas, please create an Issue

The author can be reached at [email protected]

license

LLDark uses the MIT LICENSE. For details, see the LICENSE file.