DevDragonLi / LFLDarkModeKit

暗黑模式 iOS Dark Mode Adapter Kit (Layer Color Auto Changed)
MIT License
12 stars 0 forks source link

LFLDarkModeKit

Adapter iOS DarkMode

适配支持 iOS 9.0+ 项目

Support:ObjC/Swift项目(纯OC/Swift/混编)均可使用

CI Status Version License Platform

LFLDarkModeKit Detail

Project Adapter By Apple API Description

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Installation

LFLDarkModeKit is available through CocoaPods. To install it, simply add the following line to your Podfile:


  pod 'LFLDarkModeKit', '~> 3.2.0'

LFLDarkModeKit Detail

.
├── Core
│   ├── LFLDarkModeManger.h
│   ├── LFLDarkModeManger.m
│   ├── UIColor+LFLDarkMode.h
│   ├── UIColor+LFLDarkMode.m
│   ├── UIView+LFLDarkMode.h
│   └── UIView+LFLDarkMode.m
├── LFLDarkModeKit.h
└── Tool
    ├── NSString+DarkModeKitBlank.h
    └── NSString+DarkModeKitBlank.m
    └── UIWindow+DarkModeKitKeyWondow.h
    └── UIWindow+DarkModeKitKeyWondow.m

Project create bundle source

各项目自定义色值字符串映射对应颜色

configDarkModeColorBundleURL

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     NSURL *darkModeBundleURL = [[NSBundle mainBundle] URLForResource:@"darkModeAdapterColor" withExtension:@"bundle"];
    [[LFLDarkModeManger sharedInstance] configDarkModeColorBundleURL:darkModeBundleURL];
    return YES; 
}

用户自定义模式 【可不跟随系统开关】

      // 配置开启用户自定义开关模式
    [LFLDarkModeManger.sharedInstance configUserDarkMode:YES];

     // 获取当前用户自定义是否暗黑模式
    [LFLDarkModeManger.sharedInstance isUserDarkMode];

获取状态切换【模式切换检测】


  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_darkModeNoti:) name:LFLDarkModeChangeNotification object:nil];

- (void)_darkModeNoti:(NSNotification *)noti {
NSDictionary *darkModeDic = noti.object;
NSLog(@"\n通知:暗黑模式切换检测%@",darkModeDic);
}

Use Example

// view
self.exampleLabel.textColor = [UIColor ColorAdpterWithHex:@"DEMO"];

// Imageview 
self.adapterImageView.image = [UIImage imageAdapterNamed:@"exampleImage"];

// Layer 
self.customView.layerBorderColorHex = @"DEMO";

Project Adapter By Apple API Description

全局关闭暗黑模式:在Info.plist文件中,添加UIUserInterfaceStyle key 名字为 User Interface Style 值为String,将UIUserInterfaceStyle key 的值设置为Light

images adapter

UIColor


+ (UIColor *)colorWithDynamicProvider:(UIColor * (^)(UITraitCollection *traitCollection))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchOS);

- (UIColor *)initWithDynamicProvider:(UIColor * (^)(UITraitCollection *traitCollection))dynamicProvider API_AVAILABLE(ios(13.0), tvos(13.0)) API_UNAVAILABLE(watchos);

CGColor

设置的CGColor并不会动态改变,可以通过以下方案处理

如果适配CGColor,一般需要各自自定义view实现此函数再处理,较为麻烦


- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];

if ([self.traitCollection hasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {
// Config  Color Adapter 
    }
}

- (void)traitCollectionDidChange:(UITraitCollection *)previousTraitCollection {
[super traitCollectionDidChange:previousTraitCollection];
 [self.traitCollection performAsCurrentTraitCollection:^{
    layer.backgroundColor = xxColor.CGColor;
}];
}

VC Config Single Style

if (@available(iOS 13.0, *)) {

    [self setOverrideUserInterfaceStyle:UIUserInterfaceStyleDark];

} else {

}

xib / Story

NSAttributedString

 NSDictionary * attributeDic = @{NSFontAttributeName:[UIFont systemFontOfSize:16],NSForegroundColorAttributeName:[UIColor xxColor]};

UIActivityIndicatorView

typedef NS_ENUM(NSInteger, UIActivityIndicatorViewStyle) {

    UIActivityIndicatorViewStyleMedium,

    UIActivityIndicatorViewStyleLarge,

    UIActivityIndicatorViewStyleWhiteLarge API_DEPRECATED_WITH_REPLACEMENT("UIActivityIndicatorViewStyleLarge",

    UIActivityIndicatorViewStyleWhite API_DEPRECATED_WITH_REPLACEMENT("UIActivityIndicatorViewStyleMedium",

    UIActivityIndicatorViewStyleGray API_DEPRECATED_WITH_REPLACEMENT("UIActivityIndicatorViewStyleMedium",

};

Life Style

Author

Dragonli_52171@163.com

License

LFLDarkModeKit is available under the MIT license. See the LICENSE file for more info.