您的位置:首页 > 其它

主题切换

2015-11-19 11:30 363 查看
//更主题的通知
#define ThemeChangeNotification @"ThemeChangeNotification" //更改主题的通知
#define ThemeName @"ThemeName" //主题名称

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

@interface ConfigurationTheme : NSObject{
@private
//主题配置信息
NSDictionary *_themesConfig;
}

@property (copy, nonatomic) NSString *themeName; //当前使用的主题名称
@property (strong, nonatomic) NSDictionary *themesConfig; //主题配置信息
@property (strong, nonatomic) NSDictionary *fontConfig; //字体的配置信息

//创建单例,确保该对象唯一
+ (ConfigurationTheme *)shareInstance;

//获取当前主题下的图片名称
-(UIImage *)getThemeImageName:(NSString *)imageName;
//获取当前主题下的颜色
- (UIColor *)getThemeColorWithName:(NSString *)colorName;

@end

#import "ConfigurationTheme.h"

@implementation ConfigurationTheme

//创建单例,确保该对象唯一
+ (ConfigurationTheme *)shareInstance{

static ConfigurationTheme *configuration = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
configuration = [ConfigurationTheme new];
});
return configuration;
}

//重写init方法
-(id)init{

self = [super init];
if (self != nil) {

//读取主题配置文件
NSString *filePlist = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"];
_themesConfig = [NSDictionary dictionaryWithContentsOfFile:filePlist];

//读取字体配置文件
NSString *fontConfigPlist = [[NSBundle mainBundle] pathForResource:@"fontColor" ofType:@"plist"];

_fontConfig = [NSDictionary dictionaryWithContentsOfFile:fontConfigPlist];

self.themeName = @"默认";
}

return self;
}

//得到当前主题名称
- (void)setThemeName:(NSString *)themeName{
if (_themeName != themeName) {
_themeName = [themeName copy];
}

//获取主题配置的根目录
NSString *themePath = [self getThemePath];
NSString *filePath = [themePath stringByAppendingPathComponent:@"fontColor.plist"];
_fontConfig = [NSDictionary dictionaryWithContentsOfFile:filePath];

}

//获取当前主题配置的目录
- (NSString *)getThemePath{

//项目的根路径
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];

if ([_themeName isEqualToString:@"默认"]) {
return resourcePath;
}

//获取当前主题的配置路径
NSString *configurationPath = [_themesConfig objectForKey:_themeName];

//主题的完整路径
NSString *themePath = [resourcePath stringByAppendingPathComponent:configurationPath];

return themePath;
}

//获取当前主题下的图片
-(UIImage *)getThemeImageName:(NSString *)imageName{
if (imageName.length == 0) {
return nil;
}

//获取当前主题配置的目录
NSString *configurationPath = [self getThemePath];

//图片名称在当前主题的文件路径
NSString *imagePath = [configurationPath stringByAppendingPathComponent:imageName];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];

return image;
}

//获取当前主题下的颜色
- (UIColor *)getThemeColorWithName:(NSString *)colorName{
if (colorName.length == 0){
return nil;
}

NSString *rgb = [self.fontConfig objectForKey:colorName];
NSArray *rgbs = [rgb componentsSeparatedByString:@","];

if (rgbs.count == 3)
{
float r = [rgbs[0] floatValue];
float g = [rgbs[1] floatValue];
float b = [rgbs[2] floatValue];
UIColor *color = [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1];
return color;
}

return nil;
}

+ (id)copyWithZone:(NSZone *)zone
{
return self;
}

@end
配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>默认</key>
<string>Skins/default</string>
<key>nt</key>
<string>Skins/nt</string>
</dict>
</plist>
使用:

_switchButton = [[UISwitch alloc] initWithFrame:CGRectMake(screenWidth-20-42, 15, 42, 22)];

//根据沙盒里的值来控制switch的开关
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *passWord = [ user objectForKey:@"userPassWord"];
if (!(passWord == nil) && [passWord isEqualToString:@"123456"]){
[_switchButton setOn:NO];
}else{
[_switchButton setOn:YES];
}

_switchButton.tag=indexPath.row+10001;
[_switchButton addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];

[cell.contentView addSubview:_switchButton];

-(void)switchAction:(id)sender{

//取出选中的主题名称
NSString *themeName = [ConfigurationTheme shareInstance].themeName;

if ([themeName isEqualToString:@"默认"]){
themeName = @"nt";

//往沙盒存个值
NSString *passWord = @"11111";
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:passWord forKey:@"userPassWord"];

}else{

themeName = @"默认";
//往沙盒存个值
NSString *passWord = @"123456";
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:passWord forKey:@"userPassWord"];
}

[ConfigurationTheme shareInstance].themeName = themeName;
[[NSNotificationCenter defaultCenter] postNotificationName:ThemeChangeNotification object:nil];

//保存主题到本地
[[NSUserDefaults standardUserDefaults] setObject:themeName forKey:ThemeName];
[[NSUserDefaults standardUserDefaults] synchronize];

[_myTableView reloadData];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: