pod "MSTheme"// 构建动态颜色
UIButton *btn = [UIButton new];
btn.backgroundColor = [UIColor ms_dynamicColorWithProvider:^UIColor * _Nonnull(MSAppTheme theme) {
if (theme == MSAppTheme_light) {
return [UIColor redColor];
} else {
return [UIColor yellowColor];
}
}];
// 构建动态图片
UIImageView *imageV = [UIImageView new];
imageV.image = [UIImage ms_dynamicImageWithProvider:^UIImage * _Nullable(MSAppTheme theme) {
if (theme == MSAppTheme_light) {
return [UIImage imageNamed:@"123"];
} else {
return [UIImage imageNamed:@"456"];
}
}];
// layer动态颜色
btn.layer.borderColor = [UIColor ms_dynamicColorWithProvider:^UIColor * _Nonnull(MSAppTheme theme) {
if (theme == MSAppTheme_light) {
return [UIColor redColor];
} else {
return [UIColor yellowColor];
}
}].CGColor;
btn.layer.borderWidth = 1;
// 富文本
UIColor *dynamicColor = [UIColor ms_dynamicColorWithProvider:^UIColor * _Nonnull(MSAppTheme theme) {
if (theme == MSAppTheme_light) {
return [UIColor redColor];
} else {
return [UIColor yellowColor];
}
}];
UIColor *dynamicColor2 = [UIColor ms_dynamicColorWithProvider:^UIColor * _Nonnull(MSAppTheme theme) {
if (theme == MSAppTheme_light) {
return [UIColor yellowColor];
} else {
return [UIColor redColor];
}
}];
NSAttributedString *preAttributeText = [[NSAttributedString alloc] initWithString: @"我是前缀" attributes:@{
NSForegroundColorAttributeName: dynamicColor
}];
NSAttributedString *subfixAttributeText = [[NSAttributedString alloc] initWithString:@"我是后缀" attributes:@{
NSForegroundColorAttributeName: dynamicColor2,
}];
NSMutableAttributedString *result = [preAttributeText mutableCopy];
[result appendAttributedString:subfixAttributeText];
self.textCountLabel.attributedText = result;
// 切换主题
[[MSThemeManager sharedManager] switchToTheme:MSAppTheme_light];
// 注册方法,主题切换后会自动调用
@implementation TouchSlider
+ (void)initialize {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
[MSThemeHooker registerHooksMethod:@selector(setMaxTrackColor:) forClass:TouchSlider.class];
});
}
@end
// 注册监听,主题切换后调用
[[MSThemeManager sharedManager] registerForThemeUpdate:self callback:^(MSAppTheme theme) {
[self setNeedsDisplay];
}];Alvin365, 442580988@qq.com