This commit is contained in:
2026-01-15 19:14:34 +08:00
parent 8f4deaac4e
commit b5da9f35a5
4 changed files with 31 additions and 3 deletions

View File

@@ -12,6 +12,8 @@ NS_ASSUME_NONNULL_BEGIN
@interface UIColor (Extension)
+ (UIColor *)colorWithHex:(int)hexValue;
+ (nullable UIColor *)colorWithHexString:(NSString *)hexString;
+ (UIColor *)kb_dynamicColorWithLightColor:(UIColor *)lightColor
darkColor:(UIColor *)darkColor;
@end
NS_ASSUME_NONNULL_END

View File

@@ -41,4 +41,19 @@
int rgb = (int)(value & 0xFFFFFF);
return [UIColor colorWithHex:rgb alpha:alpha];
}
+ (UIColor *)kb_dynamicColorWithLightColor:(UIColor *)lightColor
darkColor:(UIColor *)darkColor {
UIColor *light = lightColor ?: [UIColor blackColor];
UIColor *dark = darkColor ?: light;
if (@available(iOS 13.0, *)) {
return [UIColor colorWithDynamicProvider:^UIColor *_Nonnull(UITraitCollection *_Nonnull traitCollection) {
if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
return dark;
}
return light;
}];
}
return light;
}
@end