分享

IOS主题切换ThemeManager

 叹落花 2015-01-30
  1. //当前主题的名字  
  2. @property (nonatomic, copy) NSString *themeName;  
  3.   
  4. //主题图片字典, 主题名字对应的文件路径  
  5. @property (nonatomic, copy) NSDictionary *themeDic;  
  6.   
  7. //主题颜色字典  
  8. @property(nonatomic, copy) NSDictionary *fontColorDic;  
  9.   
  10.   
  11.   
  12. +(ThemeManager *)shareInstance;  
  13.   
  14. //根据业务逻辑分析,切换主题只需要切换主题图片和主题颜色,所以提供下面2个方法  
  15. -(UIImage *)loadThemeImage:(NSString *)imgName;  
  16. -(UIColor *)loadColorWithKeyName:(NSString *)themeKeyName;  
  17.   
  18. -(void)saveTheme;  



  1. #import "ThemeManager.h"  
  2.   
  3. #define kDefaultThemeName   @"默认主题"  
  4. #define kThemeName          @"kThemeName"  
  5.   
  6. @implementation ThemeManager  
  7.   
  8. static ThemeManager *instance = nil;  
  9.   
  10. +(ThemeManager *)shareInstance  
  11. {  
  12.     static dispatch_once_t onceToken;  
  13.     dispatch_once(&onceToken, ^{  
  14.         instance = [[ThemeManager alloc] init];  
  15.     });  
  16.       
  17.     return instance;  
  18. }  
  19.   
  20. - (id)init  
  21. {  
  22.     self = [super init];  
  23.     if (self) {  
  24.         NSString *themePath = [[NSBundle mainBundle] pathForResource:@"theme" ofType:@"plist"];  
  25.         _themeDic = [[NSDictionary dictionaryWithContentsOfFile:themePath] copy];  
  26.           
  27.         //从 NSUserDefaults 读取主题  
  28.         NSString *theme = [[NSUserDefaults standardUserDefaults] objectForKey:kThemeName];  
  29.         if (theme) {  
  30.             self.themeName = theme;  
  31.         }else{  
  32.             self.themeName = kDefaultThemeName;  
  33.         }  
  34.     }  
  35.     return self;  
  36. }  
  37.   
  38. - (void)dealloc  
  39. {  
  40.     [_themeName release];  
  41.     [_themeDic release];  
  42.     [_fontColorDic release];  
  43.     [super dealloc];  
  44. }  
  45.   
  46. -(NSString *)themePath  
  47. {  
  48.     //1.获取程序包的路径  
  49.     NSString *rootPath = [[NSBundle mainBundle] resourcePath];  
  50.       
  51.     //2.获取当前主题对应的路径  
  52.     NSString *themePath = [self.themeDic objectForKey:_themeName];  
  53.     return [rootPath stringByAppendingPathComponent:themePath];  
  54. }  
  55.   
  56. //切换主题  
  57. -(void)setThemeName:(NSString *)themeName  
  58. {  
  59.     if (_themeName != themeName) {  
  60.         [_themeName release];  
  61.         _themeName = [themeName copy];  
  62.           
  63.           
  64.         //读取主题颜色配置文件  
  65.         NSString *fontFilePath = [[self themePath] stringByAppendingPathComponent:@"config.plist"];  
  66.         self.fontColorDic = [NSDictionary dictionaryWithContentsOfFile:fontFilePath];  
  67.     }  
  68. }  
  69.   
  70. -(UIImage *)loadThemeImage:(NSString *)imgName  
  71. {  
  72.     if (!imgName) {  
  73.         return nil;  
  74.     }  
  75.       
  76.     NSString *imgPath = [[self themePath] stringByAppendingPathComponent:imgName];  
  77.     return [UIImage imageWithContentsOfFile:imgPath];  
  78. }  
  79.   
  80. -(UIColor *)loadColorWithKeyName:(NSString *)themeKeyName  
  81. {  
  82.     if (!themeKeyName) {  
  83.         return nil;  
  84.     }  
  85.       
  86.     NSDictionary *rgbDic = [_fontColorDic objectForKey:themeKeyName];  
  87.     float r = [[rgbDic objectForKey:@"R"] floatValue];  
  88.     float g = [[rgbDic objectForKey:@"G"] floatValue];  
  89.     float b = [[rgbDic objectForKey:@"B"] floatValue];  
  90.       
  91.     return [UIColor colorWithRed:r/255 green:g/255 blue:b/255 alpha:1];  
  92. }  
  93.   
  94. //保存主题  
  95. -(void)saveTheme  
  96. {  
  97.     [[NSUserDefaults standardUserDefaults] setObject:_themeName forKey:kThemeName];  
  98.     [[NSUserDefaults standardUserDefaults] synchronize];  
  99. }  
  100.   
  101. @end  


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多