Skip to content

Alvin365/MSTheme

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MSTheme

支持APP切换深浅色,一键完成现有项目深色模式改造

框架集成

pod "MSTheme"

原理架构图

deepseek_mermaid_20250926_fb2a3d

用法

// 构建动态颜色
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];
 }];

Author

Alvin365, 442580988@qq.com

License

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

About

支持APP切换深浅色,一键完成现有项目深色模式改造

Resources

License

Stars

Watchers

Forks

Packages

No packages published