//#import <AppKit/AppKit.h>
import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIColor (KGCDarkColorTreatment)
- (UIColor *)colorWithDarkColorStr:(NSString *)DarkColor LightColor:(NSString *)LightColor;
/// 自定义色值
/// @param DarkColor 深色模式下对应的色值
/// @param LightColor 浅色模式下对应的色值
- (UIColor *)colorWithDarkColor:(UIColor *)DarkColor LightColor:(UIColor *)LightColor;
/黑白配置/
/背景黑白/
- (UIColor )traitCollectionBackgroundColorDidChange;
/字体黑白*/ - (UIColor *)traitCollectionFontColorDidChange;
随手记录,之后会进行不合理的优化,
import "UIColor+KGCDarkColorTreatment.h"
@implementation UIColor (KGCDarkColorTreatment)
/字符串形式的/
- (UIColor *)colorWithDarkColorStr:(NSString *)DarkColor LightColor:(NSString *)LightColor
{
if (@available(iOS 13.0, *)) {
UIColor *dyColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull trainCollection) {
if ([trainCollection userInterfaceStyle] == UIUserInterfaceStyleDark) {
// NSLog(@"默认颜色,白色");
return [UIColor colorWithHexString:DarkColor];
}else {
// NSLog(@"默认颜色,黑色");
return [UIColor colorWithHexString:LightColor];
}
}];
return dyColor;
} else {
return [UIColor colorWithHexString:LightColor];
}
}
/背景颜色适配暗黑/
/黑白调用就行, 动态设置,背景和字体颜色之类的/
-
(UIColor *)traitCollectionBackgroundColorDidChange{
if (@available(iOS 13.0, *)) {
return [UIColor systemBackgroundColor];
}else{
return [UIColor whiteColor];
}
}
/暗黑字体颜色适配/
/黑白配置/
/针对渐变不同变色反向设置/
-
(UIColor *)traitCollectionFontColorDidChange{
if (@available(iOS 13.0, *)) {
UIColor *dyColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull trainCollection) {
if ([trainCollection userInterfaceStyle] == UIUserInterfaceStyleLight) {
// NSLog(@"555555");
return [UIColor blackColor];
}else {
// NSLog(@"333333");
return [UIColor whiteColor];
}
}];return dyColor;
} else {
return [UIColor blackColor];
}
}
pragma mark === /自定义色值/
- (UIColor *)colorWithDarkColor:(UIColor *)DarkColor LightColor:(UIColor *)LightColor
{
if (@available(iOS 13.0, *))
{
return [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull trait) {
return trait.userInterfaceStyle == UIUserInterfaceStyleDark ? DarkColor : LightColor;
}];
}
return LightColor;
}
网友评论