12
12
#define YA_SKIN_MANAGER_UD_KEY @" YA_SKIN_NAME"
13
13
#define YA_SKIN_CATEGORY (__KEY ) ((__KEY != nil ) ? __KEY : @" default" )
14
14
15
- @interface YASkinManager ()
15
+ @interface YASkinManager ()
16
16
17
17
@property (nonatomic , strong ) NSDictionary *skinDictionary;
18
18
@property (nonatomic , strong ) NSBundle *skinBundle;
@@ -29,10 +29,10 @@ + (instancetype)sharedManager
29
29
{
30
30
static dispatch_once_t onceToken;
31
31
dispatch_once (&onceToken, ^{
32
- NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults ];
33
- NSString *skinName = [userDefaults objectForKey: YA_SKIN_MANAGER_UD_KEY];
34
-
35
- gSharedInstance = [[YASkinManager alloc ] initWithSkinName: skinName];
32
+ NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults ];
33
+ NSString *skinName = [userDefaults objectForKey: YA_SKIN_MANAGER_UD_KEY];
34
+
35
+ gSharedInstance = [[YASkinManager alloc ] initWithSkinName: skinName];
36
36
});
37
37
return gSharedInstance ;
38
38
}
@@ -63,10 +63,10 @@ - (void)setSkinName:(NSString *)skinName
63
63
_skinName = (skinName != nil ) ? skinName : @" DefaultSkin" ;
64
64
_skinBundle = [NSBundle bundleWithURL: [[NSBundle mainBundle ] URLForResource: _skinName withExtension: @" bundle" ]];
65
65
NSAssert (_skinBundle != nil , @" Skin Bundle should not be nil!" );
66
-
66
+
67
67
NSURL *skinFileURL = [_skinBundle URLForResource: @" config" withExtension: @" .plist" ];
68
68
self.skinDictionary = [NSDictionary dictionaryWithContentsOfURL: skinFileURL];
69
-
69
+
70
70
if (self == gSharedInstance ) {
71
71
[[NSUserDefaults standardUserDefaults ] setObject: _skinName forKey: YA_SKIN_MANAGER_UD_KEY];
72
72
[[NSUserDefaults standardUserDefaults ] synchronize ];
@@ -83,13 +83,12 @@ - (UIColor *)colorForTag:(NSString *)tag
83
83
if (!colorHex) {
84
84
colorHex = [colorDict objectForKey: YA_SKIN_CATEGORY (nil )];
85
85
}
86
-
86
+
87
87
UIColor *color = nil ;
88
88
if ([colorHex isKindOfClass: [NSNumber class ]]) {
89
89
color = [UIColor colorWithHex: [colorHex integerValue ]];
90
- } else if ([colorHex isKindOfClass: [NSString class ]]) {
91
- color = [UIColor colorWithHexString: colorHex];
92
90
}
91
+
93
92
return color;
94
93
}
95
94
@@ -100,13 +99,13 @@ - (UIFont *)fontForTag:(NSString *)tag
100
99
if (fontFamily == nil ) {
101
100
fontFamily = [fontFamilyDict objectForKey: YA_SKIN_CATEGORY (nil )];
102
101
}
103
-
102
+
104
103
NSDictionary *fontSizeDict = self.skinDictionary [@" Font-Size" ];
105
- NSNumber *fontSize = [fontSizeDict objectForKey: YA_SKIN_CATEGORY (tag)];
104
+ NSNumber *fontSize = [fontSizeDict objectForKey: YA_SKIN_CATEGORY (tag)];
106
105
if (fontSize == nil ) {
107
106
fontSize = [fontSizeDict objectForKey: YA_SKIN_CATEGORY (nil )];
108
107
}
109
-
108
+
110
109
return [UIFont fontWithName: fontFamily size: fontSize.floatValue];
111
110
}
112
111
@@ -121,9 +120,10 @@ - (UIImage *)imageWithName:(NSString *)imageName cache:(BOOL)flag
121
120
if (cachedImage == nil ) {
122
121
NSString *filename = [imageName stringByDeletingPathExtension ];
123
122
NSString *extension = [imageName pathExtension ];
124
- if ([extension length ] == 0 ) extension = @" png" ;
123
+ if ([extension length ] == 0 )
124
+ extension = @" png" ;
125
125
NSBundle *bundle = self.skinBundle ;
126
-
126
+
127
127
NSString *imagePath = nil ;
128
128
if ([UIScreen mainScreen ].scale == 3.0 ) {
129
129
imagePath = [self ya_plusImagePathWithName: filename extension: extension inBundle: bundle];
@@ -133,21 +133,24 @@ - (UIImage *)imageWithName:(NSString *)imageName cache:(BOOL)flag
133
133
NSString *imageForiPhone5 = [NSString stringWithFormat: @" %@ -568h@2x" , filename];
134
134
imagePath = [bundle pathForResource: imageForiPhone5 ofType: extension];
135
135
}
136
-
136
+
137
137
// If no image specified for iPhone 5, fallback to normal retina image.
138
- if (imagePath == nil ) imagePath = [self __retainImagePathWithName: filename extension: extension inBundle: bundle];
139
-
138
+ if (imagePath == nil )
139
+ imagePath = [self __retainImagePathWithName: filename extension: extension inBundle: bundle];
140
+
140
141
// If no image specified for Retain, fallback to normal image.
141
- if (imagePath == nil ) imagePath = [bundle pathForResource: filename ofType: extension];
142
+ if (imagePath == nil )
143
+ imagePath = [bundle pathForResource: filename ofType: extension];
142
144
} else {
143
145
imagePath = [bundle pathForResource: filename ofType: extension];
144
-
146
+
145
147
// If no image For Normal, fallback to Normal Retain Image.
146
- if (imagePath == nil ) imagePath = [self __retainImagePathWithName: filename extension: extension inBundle: bundle];
148
+ if (imagePath == nil )
149
+ imagePath = [self __retainImagePathWithName: filename extension: extension inBundle: bundle];
147
150
}
148
-
151
+
149
152
UIImage *image = (imagePath != nil ) ? [UIImage imageWithContentsOfFile: imagePath] : nil ;
150
-
153
+
151
154
if (image == nil ) {
152
155
NSLog (@" [PASkinManager] Failed to get image with name: %@ " , imageName);
153
156
} else {
@@ -169,12 +172,11 @@ - (void)cleanImageCache
169
172
170
173
#pragma mark - Private
171
174
172
- - (NSString *)ya_plusImagePathWithName : (NSString *)imageName
173
- extension : (NSString *)extension
174
- inBundle : (NSBundle *)bundle
175
+ - (NSString *)ya_plusImagePathWithName : (NSString *)imageName extension : (NSString *)extension inBundle : (NSBundle *)bundle
175
176
{
176
177
NSString *retinaName = [NSString stringWithFormat: @" %@ @3x" , imageName];
177
- if (bundle == nil ) bundle = [NSBundle mainBundle ];
178
+ if (bundle == nil )
179
+ bundle = [NSBundle mainBundle ];
178
180
return [bundle pathForResource: retinaName ofType: extension];
179
181
}
180
182
@@ -183,7 +185,8 @@ - (NSString *)__retainImagePathWithName:(NSString *)imageName
183
185
inBundle : (NSBundle *)bundle
184
186
{
185
187
NSString *retinaName = [NSString stringWithFormat: @" %@ @2x" , imageName];
186
- if (bundle == nil ) bundle = [NSBundle mainBundle ];
188
+ if (bundle == nil )
189
+ bundle = [NSBundle mainBundle ];
187
190
return [bundle pathForResource: retinaName ofType: extension];
188
191
}
189
192
0 commit comments