ios 主题切换 思路_IOS主题切换ThemeManager
#import?"ThemeManager.h"
#define?kDefaultThemeName???@"默認(rèn)主題"
#define?kThemeName??????????@"kThemeName"
@implementation?ThemeManager
static?ThemeManager?*instance?=?nil;
+(ThemeManager?*)shareInstance
{
static?dispatch_once_t?onceToken;
dispatch_once(&onceToken,?^{
instance?=?[[ThemeManager?alloc]?init];
});
return?instance;
}
-?(id)init
{
self?=?[super?init];
if?(self)?{
NSString?*themePath?=?[[NSBundle?mainBundle]?pathForResource:@"theme"?ofType:@"plist"];
_themeDic?=?[[NSDictionary?dictionaryWithContentsOfFile:themePath]?copy];
//從?NSUserDefaults?讀取主題
NSString?*theme?=?[[NSUserDefaults?standardUserDefaults]?objectForKey:kThemeName];
if?(theme)?{
self.themeName?=?theme;
}else{
self.themeName?=?kDefaultThemeName;
}
}
return?self;
}
-?(void)dealloc
{
[_themeName?release];
[_themeDic?release];
[_fontColorDic?release];
[super?dealloc];
}
-(NSString?*)themePath
{
//1.獲取程序包的路徑
NSString?*rootPath?=?[[NSBundle?mainBundle]?resourcePath];
//2.獲取當(dāng)前主題對(duì)應(yīng)的路徑
NSString?*themePath?=?[self.themeDic?objectForKey:_themeName];
return?[rootPath?stringByAppendingPathComponent:themePath];
}
//切換主題
-(void)setThemeName:(NSString?*)themeName
{
if?(_themeName?!=?themeName)?{
[_themeName?release];
_themeName?=?[themeName?copy];
//讀取主題顏色配置文件
NSString?*fontFilePath?=?[[self?themePath]?stringByAppendingPathComponent:@"config.plist"];
self.fontColorDic?=?[NSDictionary?dictionaryWithContentsOfFile:fontFilePath];
}
}
-(UIImage?*)loadThemeImage:(NSString?*)imgName
{
if?(!imgName)?{
return?nil;
}
NSString?*imgPath?=?[[self?themePath]?stringByAppendingPathComponent:imgName];
return?[UIImage?imageWithContentsOfFile:imgPath];
}
-(UIColor?*)loadColorWithKeyName:(NSString?*)themeKeyName
{
if?(!themeKeyName)?{
return?nil;
}
NSDictionary?*rgbDic?=?[_fontColorDic?objectForKey:themeKeyName];
float?r?=?[[rgbDic?objectForKey:@"R"]?floatValue];
float?g?=?[[rgbDic?objectForKey:@"G"]?floatValue];
float?b?=?[[rgbDic?objectForKey:@"B"]?floatValue];
return?[UIColor?colorWithRed:r/255?green:g/255?blue:b/255?alpha:1];
}
//保存主題
-(void)saveTheme
{
[[NSUserDefaults?standardUserDefaults]?setObject:_themeName?forKey:kThemeName];
[[NSUserDefaults?standardUserDefaults]?synchronize];
}
@end
總結(jié)
以上是生活随笔為你收集整理的ios 主题切换 思路_IOS主题切换ThemeManager的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql 5.1.53_mysql 5
- 下一篇: linux的vim多选择,真的有(很多)