多主题平滑切换的快速集成框架(Theme change)
多主题平滑切换的快速集成框架(Theme change)
下载文件直接将HZThemeManager文件夹添加到项目中
1.在资源包([NSBundle mainBundle])下建立多个独立的资源文件夹,存放对应的资源文件,必须要有bgColor.plist、textColor.plist这2个文件。
2.同一类型(指在同一个视图身上)不同样式的UI数据(UIColor,UIImage)用同一个标识符
3.ThemeManager会根据当前的主题去对应的文件夹下获取标识符所对应的数据
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
/**
* 主题文件夹路径配置
*/
[ThemeManager sharedManager].themePaths = @{
@"black":@"Theme/Black",
@"white":@"Theme/White"
};
/**
* 设置当前主题
*/
[ThemeManager sharedManager].themeName = [[NSUserDefaults standardUserDefaults] objectForKey:@"theme"]?:@"white";
return YES;
}
/**
* 从ThemeManager获取UI数据
*/
self.view.backgroundColor = [[ThemeManager sharedManager] getThemeBgColorWithName:@"baseBgColor"];
/**
* 获取当前主题下 imageName对应的图片
* imageName:图片类型标识
*/
- (UIImage *)getThemeImageWithName:(NSString *)imageName;
/**
* 获取当前主题下 textColorName对应的颜色
* textColorName:文字颜色类型标识
*/
- (UIColor *)getThemeTextColorWithName:(NSString *)textColorName;
/**
* 注册通知,主题改变时调用
*/
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChangeNotification:) name:kThemeDidChangeNotification object:nil];
/**
* 创建文本Label类型
* textColorName:颜色类型的标识符
* 文本类型默认为NSString
*/
UILabel *themeLabel = [UIFactory themeLabelWithTextColorName:@"titleColor"];
themeLabel.text = @"开启夜间模式:";
themeLabel.font = [UIFont systemFontOfSize:16];
[self.view addSubview:themeLabel];
[themeLabel sizeToFit];
themeLabel.centerY = themeChange.centerY;
[themeLabel rightOverView:themeChange offset:-10];
/**
* 创建图片ImageView类型
* imageName:图片类型的标识符
*/
UIImageView *themeImageView = [UIFactory themeImgViewWithImageName:@"discover_enter"];
themeImageView.frame = CGRectMake(0, 0, 60, 25);
themeImageView.center = CGPointMake(self.view.width/2, 250);
[self.view addSubview:themeImageView];
/**
* 创建按钮Button类型
* imageName:图片类型的标识符
*/
UIButton *themeBtn = [UIFactory themeButtonWithImageName:@"topicDownloadImg"];
themeBtn.frame = CGRectMake(0, 0, 42, 42);
themeBtn.center = CGPointMake(self.view.width/2, 200);
[self.view addSubview:themeBtn];
/**
* 创建普通View类型
* bgColorName:颜色类型的标识符
*/
+ (ThemeView *)themeViewWithBgColorName:(NSString *)bgColorName;