iOS控件---UI适配器切换主题
2017-06-07 本文已影响42人
7965f47b74d9
一、需求:
1、一键切换APP的主题
二、思路:
主题所包含的内容有:字体、字体大小、颜色、图片,所以需要在切换主题的时候主要把这些东西都换掉
1、把不同的主题用文件夹装起来,所以需要解决路径问题
2、字体、字体大小、颜色用一个plist进行设置,放在主题文件夹里
3、每个主题文件夹的图片名字一样,只需要改变路径就可以改变图片
4、把页面的需要随主题改变的字体、字体大小、颜色、图片的设置放到block中,然后调用block到达切换主题的目的
5、用一个单例管理来管理主题的配置
三、Code:
1、默认主题和不同主题的对应路径
//主题对应的路径
- (NSString *)topicPath:(NSString *)name
{
if ([self.topicName isEqualToString:WXHUIAdapterTopicNameDefault]) {
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
bundlePath = [bundlePath stringByAppendingPathComponent:name];
return bundlePath;
} else {
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
documentPath = [documentPath stringByAppendingPathComponent:WXHUIAdapterTopicManager];
documentPath = [documentPath stringByAppendingPathComponent:self.topicName];
documentPath = [documentPath stringByAppendingPathComponent:name];
return documentPath;
}
}
2、根据不同主题获取对应的内容
//字体、字体大小、颜色的配置信息
- (void)loadTopicData
{
NSString *infoPlistPath = [self topicPath:WXHUIAdapterTopicInfoPlist];
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:infoPlistPath];
self.sizeDic = dic[@"size"];
self.fontDic = dic[@"font"];
self.colorDic = dic[@"color"];
}
//字体大小
+ (CGFloat)topicSize:(NSString *)fontName
{
WXHUIAdapter *sharedWXHUIAdapter = [WXHUIAdapter sharedWXHUIAdapter];
NSString *size = sharedWXHUIAdapter.sizeDic[fontName];
return size.floatValue;
}
//字体
+ (UIFont *)topicFont:(NSString *)fontName
{
WXHUIAdapter *sharedWXHUIAdapter = [WXHUIAdapter sharedWXHUIAdapter];
NSString *name = sharedWXHUIAdapter.fontDic[fontName];
CGFloat size = [self topicSize:fontName];
if (fontName) {
return [UIFont fontWithName:name size:size];
} else {
return [UIFont systemFontOfSize:size];
}
}
//颜色
+ (UIColor *)topicColor:(NSString *)colorName
{
WXHUIAdapter *sharedWXHUIAdapter = [WXHUIAdapter sharedWXHUIAdapter];
NSString *color = sharedWXHUIAdapter.colorDic[colorName];
return [UIColor colorWithHexString:color];
}
//16进制颜色
+ (NSString *)topicColorString:(NSString *)colorName
{
WXHUIAdapter *sharedWXHUIAdapter = [WXHUIAdapter sharedWXHUIAdapter];
NSString *color = sharedWXHUIAdapter.colorDic[colorName];
if (color) {
return color;
} else {
return @"#FFFFFF";
}
}
//图片
+ (UIImage *)topicImage:(NSString *)imageName
{
WXHUIAdapter *sharedWXHUIAdapter = [WXHUIAdapter sharedWXHUIAdapter];
if ([sharedWXHUIAdapter.topicName isEqualToString:WXHUIAdapterTopicNameDefault]) {
return [UIImage imageNamed:imageName];
} else {
NSString *imagePath = [sharedWXHUIAdapter topicPath:imageName];
return [UIImage imageWithContentsOfFile:imagePath];
}
}
3、注册/删除一个主题改变时调用的block
//注册一个主题改变时调用的block
+ (void)registTopicDidChange:(TopicDidChangeBlock)topicDidChangeBlock forKey:(NSString *)key
{
if (key) {
WXHUIAdapter *sharedWXHUIAdapter = [WXHUIAdapter sharedWXHUIAdapter];
if (topicDidChangeBlock) {
WXHUIAdapterChangeBlockInfo *blockInfo = [[WXHUIAdapterChangeBlockInfo alloc] init];
blockInfo.topicDidChangeBlock = topicDidChangeBlock;
blockInfo.key = key;
[sharedWXHUIAdapter.changeBlockDic setObject:blockInfo forKey:key];
topicDidChangeBlock(sharedWXHUIAdapter.topicName);
} else {
[sharedWXHUIAdapter.changeBlockDic removeObjectForKey:key];
}
}
}
//删除已经注册的block
+ (void)removeTopicDidChangeForKey:(NSString *)key
{
if (key) {
WXHUIAdapter *sharedWXHUIAdapter = [WXHUIAdapter sharedWXHUIAdapter];
[sharedWXHUIAdapter.changeBlockDic removeObjectForKey:key];
}
}
4、切换主题
//设置主题
+ (void)setTopic:(NSString *)topicName
{
WXHUIAdapter *sharedWXHUIAdapter = [WXHUIAdapter sharedWXHUIAdapter];
sharedWXHUIAdapter.topicName = topicName;
}
- (void)setTopicName:(NSString *)topicName
{
if (_topicName != topicName) {
_topicName = topicName;
[[NSUserDefaults standardUserDefaults] setObject:topicName forKey:WXHUIAdapterTopicName];
[[NSUserDefaults standardUserDefaults] synchronize];
[sharedWXHUIAdapter loadTopicData];
NSArray *array = [sharedWXHUIAdapter.changeBlockDic allValues];
for (WXHUIAdapterChangeBlockInfo *blockInfo in array) {
blockInfo.topicDidChangeBlock(sharedWXHUIAdapter.topicName);
}
}
}
总结:
1、实现的方式比较简单,图片除了默认的主题会有缓存,其他的主题的图片都没做缓存,可以按实际需求添加。
2、每一个主题都需要把所有的对应数据都保存一次,如果找不到数据就默认置空了。
3、对应单个页面的字体、字体大小的改变,如web页面的字体、聊天的字体设置,不属于主题的部分最好单独管理