- 换肤原理:
- 1.切换背景图片或者控件的背景色
- 2.更改文字颜色
- 需用到偏好设置存储上一次的设置
- 需注意是如果程序意外退出,数据不会被系统写入到该文件,不过可以使用
[[NSUserDefaults standardUserDefaults] synchronize]
命令直接同步到文件里,来避免数据的丢失。
- 需注意是如果程序意外退出,数据不会被系统写入到该文件,不过可以使用
- 更改控件的背景色用到plist存储RGB颜色值
- 使用换肤要写在控制器的
viewWillAppear
方法中,避免换肤没有起作用比如tabBarController
- 用到UIAppearance需要注意:
- UIAppearance有个限制,就是如果想让它生效,必须在下次装载入app的主窗口时才能生效,所以,如果要通过UIAppearance动态修改组件的属性,那么可以把整个控制器的View先移到一边,然后再添加到主窗口;
1.封装的工具类,对外提供接口
#import <UIKit/UIKit.h>
#define skinColorKey @"skinColor"
@interface SkinTool : NSObject
+ (void)setSkincolor:(NSString *)skinColor;
+ (UIImage *)skinToolWithImageName:(NSString *)imageName;
+ (UIColor *)skinToolWithLabelColor;
@end
#import "SkinTool.h"
@implementation SkinTool
static NSString *_skinColor;
// 这个类第一次使用的时候调用,而且只会调用一次
+ (void)initialize
{
NSString *value = [[NSUserDefaults standardUserDefaults] objectForKey:skinColorKey];
if (value == nil) {
value = @"red";
}
_skinColor = value;
}
+ (void)setSkincolor:(NSString *)skinColor
{
_skinColor = skinColor;
// 保存用户选中的皮肤颜色
[[NSUserDefaults standardUserDefaults] setObject:skinColor forKey:skinColorKey];
}
[[NSUserDefaults standardUserDefaults] synchronize];
// 返回一张图片
+ (UIImage *)skinToolWithImageName:(NSString *)imageName
{
// 拼接图片路径
NSString *imagePath = [NSString stringWithFormat:@"skin/%@/%@",_skinColor,imageName];
return [UIImage imageNamed:imagePath];
}
// 返回一个plist文件中RGB颜色
+ (UIColor *)skinToolWithLabelColor
{
// 1.获取对应plist文件名称
NSString *plistName = [NSString stringWithFormat:@"skin/%@/bgColor.plist",_skinColor];
// 2.获取对应plist文件路径
NSString *plistPath = [[NSBundle mainBundle] pathForResource:plistName ofType:nil];
// 3.读取plist文件
NSDictionary *colorDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
NSString *colorStr = colorDict[@"labelBgColor"];
// 4.获取颜色数组
NSArray *colorArr = [colorStr componentsSeparatedByString:@","];
// 5.读取对应RGB
NSInteger red = [colorArr[0] integerValue];
NSInteger green = [colorArr[1] integerValue];
NSInteger blue = [colorArr[2] integerValue];
return [UIColor colorWithRed:red / 255.0 green:green / 255.0 blue:blue / 255.0 alpha:1.0];
}
2.在第一个控制器中
- (void)viewDidLoad {
[super viewDidLoad];
[self changeImage];
[self setLabelTextColorAndButtonTitleColor];
}
- (void)setLabelTextColorAndButtonTitleColor{
UIColor *color = [SkinTool skinToolWithLabelColor];
[[UIButton appearance] setTitleColor:color forState:UIControlStateNormal];
[[UILabel appearance] setTextColor:color];
}
- (IBAction)changeRedColor:(id)sender {
[SkinTool setSkincolor:@"red"];
[self setLabelTextColorAndButtonTitleColor];
[self.tabBarController.view removeFromSuperview];
[[UIApplication sharedApplication].keyWindow addSubview:self.tabBarController.view];
[self changeImage];
}
- (IBAction)changeGreenColor:(id)sender {
[SkinTool setSkincolor:@"green"];
[self setLabelTextColorAndButtonTitleColor];
[self.tabBarController.view removeFromSuperview];
[[UIApplication sharedApplication].keyWindow addSubview:self.tabBarController.view];
[self changeImage];
}
- (IBAction)changeBlueColor:(id)sender {
[SkinTool setSkincolor:@"blue"];
[self setLabelTextColorAndButtonTitleColor];
[self.tabBarController.view removeFromSuperview];
[[UIApplication sharedApplication].keyWindow addSubview:self.tabBarController.view];
[self changeImage];
}
- (IBAction)chageOrangecolor:(id)sender {
[SkinTool setSkincolor:@"orange"];
[self changeImage];
}
- (void)changeImage
{
self.faceImageView.image = [SkinTool skinToolWithImageName:@"face"];
self.heartImageView.image = [SkinTool skinToolWithImageName:@"heart"];
self.rectImageView.image = [SkinTool skinToolWithImageName:@"rect"];
}
3.在第二个控制器中
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self changeImages];
self.testLabel.backgroundColor = [SkinTool skinToolWithLabelColor];
}
- (void)changeImages
{
self.faceImageView.image = [SkinTool skinToolWithImageName:@"face"];
self.heartImageView.image = [SkinTool skinToolWithImageName:@"heart"];
self.rectImageView.image = [SkinTool skinToolWithImageName:@"rect"];
}
plist中RGB配置示例图
测试效果如图