ThemeManager主题切换(1)
2015-07-22 20:14
746 查看
主题包文件:使用实体文件拖入Xcode
主题包plist文件:
主题类.h
#import <Foundation/Foundation.h>
//通知名称
#define ThemeNameChange @"ThemeNameChange"
//默认主题
#define ThemeDefault @"bluemoon"
//保存到本地的key
#define kThemeName @"kThemeName"
@interface ThemeManager : NSObject
/**单例*/
+ (ThemeManager *)shareInstance;
/**当前主题*/
@property(nonatomic, copy)NSString *themeName;
/**主题图片字典*/
@property(nonatomic, strong)NSDictionary *themeDic;
/**主题字体颜色字典*/
@property (nonatomic ,strong) NSDictionary *fontColorDic;
/**根据当前的需要的图片名获取图片*/
- (UIImage *)getThemeImg:(NSString *)imgName;
/**根据传入的key获取RGB颜色*/
- (UIColor *)getThemeColor:(NSString *)colorKeyName;
/**保存主题*/
- (void)saveTheme;
@end
主题类.m
@implementation ThemeManager
//1.定义一个单例 返回一个主题管理对象
+ (ThemeManager *)shareInstance {
static dispatch_once_t onceToken;
static ThemeManager *instance = nil;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});
return instance;
}
//2.在对象初始化的时候,设置默认主题、读取本地存取的主题、读取plist内容存入属性字典
- (instancetype)init
{
if (self = [super init]) {
_themeName = ThemeDefault;
// 1.从 NSUserDefaults 读取主题
NSString *theme = [[NSUserDefaults standardUserDefaults] objectForKey:kThemeName];
if (theme.length > 0) {
//如果本地有值,拿到本地数据赋值
_themeName = theme;
}
// 2.获取主题图片配置文件
NSString *themeData = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"];
self.themeDic = [NSDictionary dictionaryWithContentsOfFile:themeData];
// 3.读取主题字体颜色配置文件
NSString *fontFilePath = [[self themePath] stringByAppendingPathComponent:@"config.plist"];
self.fontColorDic = [NSDictionary dictionaryWithContentsOfFile:fontFilePath];
}
return self;
}
//3.通过传入主题名称设置主题,先保存主题,再读取字体的配置文件,发送通知
- (void)setThemeName:(NSString *)themeName {
if (_themeName != themeName) {
_themeName = themeName;
// 保存之前的主题
[self saveTheme];
// 读取主题字体颜色配置文件
NSString *fontFilePath = [[self themePath] stringByAppendingPathComponent:@"config.plist"];
self.fontColorDic = [NSDictionary dictionaryWithContentsOfFile:fontFilePath];
// 发送主题切换的通知
[[NSNotificationCenter defaultCenter] postNotificationName:ThemeNameChange object:self];
}
}
//4.根据传入主题名称来获取主题图片
- (UIImage *)getThemeImg:(NSString *)imgName {
// 如果传入空值
if (imgName.length == 0) {
return nil;
}
// 获取主题包得路径/....skins/cat/图片名称(获得图片路径)
NSString *imagePath = [[self themePath] stringByAppendingPathComponent:imgName];
// 通过路径获得图片
UIImage *img = [UIImage imageWithContentsOfFile:imagePath];
return img;
}
//5.根据传入的字体颜色的key来获取主题字体
-(UIColor *)getThemeColor:(NSString *)themeKeyName
{
// 如果传入空值
if (themeKeyName.length == 0) {
return nil;
}
//通过主题key取值 还是字典
NSDictionary *rgbDic = [_fontColorDic objectForKey:themeKeyName];
CGFloat r = [rgbDic[@"R"] floatValue];
CGFloat g = [rgbDic[@"G"] floatValue];
CGFloat b = [rgbDic[@"B"] floatValue];
CGFloat alpha = [rgbDic[@"alpha"] floatValue];
if (rgbDic[@"alpha"] == nil) {
alpha = 1;
}
return [UIColor colorWithRed:r/255 green:g/255 blue:b/255 alpha:1];
}
//获得主题包
- (NSString *)themePath
{
// 取得主题路径
//(1)取得程序包的根路径
NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
//(2)获取图片的路径 skins/cat
NSString *imgPath = [_themeDic objectForKey:_themeName];
//(3)获取主题包得路径/....skins/cat
return [resourcePath stringByAppendingPathComponent:imgPath];
}
//保存主题
-(void)saveTheme
{
[[NSUserDefaults standardUserDefaults] setObject:_themeName forKey:kThemeName];
[[NSUserDefaults standardUserDefaults] synchronize];
}
UIImageView子类化及使用 XLUIImageView.h @interface XLUIImageView : UIImageView @property (nonatomic ,copy) NSString *imgName; @end XLUIImageView.m #import "XLUIImageView.h" #import "ThemeManager.h" @implementation XLUIImageView - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadView) name:ThemeNameChange object:nil]; } return self; } - (void)awakeFromNib { [super awakeFromNib]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadView) name:ThemeNameChange object:nil]; } - (void)setImgName:(NSString *)imgName { if (_imgName != imgName) { _imgName = imgName; [self loadView]; } } - (void)loadView { self.image = [[ThemeManager shareManager] getThemeImg:_imgName]; } @end 使用: _selectedImage.imgName = @"home_bottom_tab_arrow.png"; _tabBarView.imgName = @"mask_navbar.png";
UIButton子类化 XLUIButton.h @interface XLUIButton : UIButton @property (nonatomic, copy) NSString *normalImage; @property (nonatomic ,copy) NSString *highlightedImage; @property (nonatomic ,copy) NSString *normalBgImage; @property (nonatomic ,copy) NSString *highlightedBgImage; @end XLUIButton.m #import "XLUIButton.h" #import "ThemeManager.h" @implementation XLUIButton - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:ThemeNameChange object:nil]; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadImage) name:ThemeNameChange object:nil]; } return self; } - (void)awakeFromNib { [self awakeFromNib]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadImage) name:ThemeNameChange object:nil]; } - (void)setNormalImage:(NSString *)normalImage { if (_normalImage != normalImage) { _normalImage = normalImage; [self loadImage]; } } - (void)setNormalBgImage:(NSString *)normalBgImage { if (_normalBgImage != normalBgImage) { _normalBgImage = normalBgImage; [self loadImage]; } } - (void)setHighlightedImage:(NSString *)highlightedImage { if (_highlightedImage != highlightedImage) { _highlightedImage = highlightedImage; [self loadImage]; } } - (void)setHighlightedBgImage:(NSString *)highlightedBgImage { if (_highlightedBgImage != highlightedBgImage) { _highlightedBgImage = highlightedBgImage; [self loadImage]; } } - (void)loadImage { ThemeManager *theme = [ThemeManager shareManager]; UIImage *norImg = [theme getThemeImg:self.normalImage]; UIImage *norBgImg = [theme getThemeImg:self.normalBgImage]; UIImage *highImg = [theme getThemeImg:self.highlightedImage]; UIImage *hightBgImg = [theme getThemeImg:self.highlightedBgImage]; if (norImg) { [self setImage:norImg forState:UIControlStateNormal]; } if (norBgImg) { [self setImage:norBgImg forState:UIControlStateNormal]; } if (highImg) { [self setImage:highImg forState:UIControlStateHighlighted]; } if (hightBgImg) { [self setImage:hightBgImg forState:UIControlStateHighlighted]; } } @end XLUIButton的使用 XLUIButton *button = [[XLUIButton alloc] initWithFrame:CGRectMake(buttonX, buttonY, buttonW, buttonH)]; //设置默认状态的图片 button.normalImage = btnImage[i];
UILable子类化 XLUILable.h @interface XLUILable : UILabel @property (nonatomic ,copy) NSString *colorName; @end XLUILable.m #import "XLUILable.h" #import "ThemeManager.h" @implementation XLUILable - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:ThemeNameChange object:nil]; } - (instancetype)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // 接收通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadColor) name:ThemeNameChange object:nil]; } return self; } - (void)awakeFromNib { [super awakeFromNib]; // 接收通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadColor) name:ThemeNameChange object:nil]; } - (void)setColorName:(NSString *)colorName { if (_colorName != colorName) { _colorName = colorName; [self loadColor]; } } - (void)loadColor { self.textColor = [[ThemeManager shareManager] getThemeColor:_colorName]; } @end XLUILable使用 XLUILable *lable = [[XLUILable alloc] initWithFrame:CGRectMake(10, 50, 200, 80)]; [self.view addSubview:lable]; lable.colorName = @"Mask_Title_color"; lable.text = @"11111";
导航栏和基类viewController的图片和字体颜色的设置
- (void)viewDidLoad { [super viewDidLoad]; [self colorChang]; } - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:ThemeNameChange object:nil]; } - (instancetype)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(colorChang) name:ThemeNameChange object:nil]; } return self; } - (void)colorChang { NSString *image = nil; if (iOS7) { image = @"mask_titlebar64@2x.png"; }else{ image = @"mask_titlebar@2x.png"; } //获取图片 UIImage *img = [[ThemeManager shareManager] getThemeImg:image]; //设置背景图片 [self.navigationBar setBackgroundImage:img forBarMetrics:UIBarMetricsDefault]; //设置字体颜色 UIColor *color = [[ThemeManager shareManager] getThemeColor:@"Mask_Title_color"]; NSDictionary *attrDict = @{ NSFontAttributeName: [UIFont fontWithName: @"Zapfino" size: 20], NSForegroundColorAttributeName: color }; self.navigationBar.titleTextAttributes = attrDict; }
@implementation BaseViewController - (void)viewDidLoad { [super viewDidLoad]; //初始化时设置默认主题背景图片 [self bgColorChang]; } //删除通知 - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self name:ThemeNameChange object:nil]; } //通过storyboard创建控制器时调用 - (instancetype)initWithCoder:(NSCoder *)aDecoder { if (self = [super initWithCoder:aDecoder]) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bgColorChang) name:ThemeNameChange object:nil]; } return self; } //通知触发方法 - (void)bgColorChang { ThemeManager *theme = [ThemeManager shareManager]; self.view.backgroundColor = [UIColor colorWithPatternImage:[theme getThemeImg:@"bg_home.jpg"]]; }
Demo链接: http://pan.baidu.com/s/1i3gthyt 密码: ytxu
相关文章推荐
- 护理宝宝皮肤“土”法大汇集
- ASP、PHP与javascript根据时段自动切换CSS皮肤的代码
- JQuery给网页更换皮肤的方法
- iOS开发之路--微博“更多”页面
- python实现360皮肤按钮控件示例
- iOS开发之路--微博骨架搭建
- IOS开发代码分享之获取启动画面图片的string
- IOS开发之路--C语言数组和字符串
- IOS开发代码分享之用nstimer实现倒计时功能
- IOS开发代码分享之设置UISearchBar的背景颜色
- ios开发之环境搭建-01
- Win8 Skin Pack——让你提前感受Win8的降临
- 在开发iOS程序时对日期处理的总结
- iOS开发——keychain的使用
- iOS Graphics Technologies iOS 图形技术
- iOS开发异常错误总结
- VCL Styles
- Flex中的皮肤(一)
- Flex中的皮肤(二)
- 学习Objective-C:入门手册