Skip to content

Commit 9596208

Browse files
author
jrapoport
committed
Merge remote-tracking branch 'appwilldev/master'
2 parents f9107d6 + e739bcd commit 9596208

File tree

12 files changed

+252
-18
lines changed

12 files changed

+252
-18
lines changed

AWLThemeManager.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |s|
1616
#
1717

1818
s.name = "AWLThemeManager"
19-
s.version = "1.0.3"
19+
s.version = "1.0.4"
2020
s.summary = "AWLThemeManager is a lightweight theme manager for iOS."
2121
s.homepage = "https://github.com/appwilldev/AWLThemeManager"
2222
# s.screenshots = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"

AWLThemeManager/AWLThemeManager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
- (UIFont*)fontForKey:(NSString*)key;
2626
- (UIFont *)fontForKey:(NSString *)key forTheme:(NSString*)themeName;
2727

28-
//Get img from theme bundle
29-
- (UIImage*)imageNamed:(NSString*)imgName;
30-
- (UIImage *)imageNamed:(NSString *)imgName forTheme:(NSString*)themeName;
28+
//Get img name from defaults.plist, and then img from theme bundle. Use key as image name if img name is missed.
29+
- (UIImage*)imageNamed:(NSString*)key;
30+
- (UIImage *)imageNamed:(NSString *)key forTheme:(NSString*)themeName;
3131

3232
//key in defaults.plist of the current theme
3333
- (id)objectForKey:(NSString*)key;

AWLThemeManager/AWLThemeManager.m

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ - (UIColor *)colorForKey:(NSString *)key forTheme:(NSString *)themeName
8888
NSString *colorValue = [self objectForKey:key forTheme:themeName];
8989
UIColor *color = [self colorFromString:colorValue];
9090
if (color == nil && [self isValidString:colorValue]) {
91-
NSArray* referenceColor = [colorValue componentsSeparatedByString: @":"];
91+
NSArray* referenceColor = [colorValue componentsSeparatedByString:@":"];
9292
colorValue = referenceColor.firstObject;
9393
color = [self colorForKey:colorValue forTheme:themeName];
9494
if (referenceColor.count > 1) {
95-
color = [color colorWithAlphaComponent: [referenceColor[1] doubleValue]];
95+
color = [color colorWithAlphaComponent:[referenceColor[1] doubleValue]];
9696
}
9797
}
9898

@@ -118,21 +118,48 @@ - (UIColor*)colorFromString:(NSString*)colorValue
118118
return nil;
119119
}
120120

121-
- (UIImage *)imageNamed:(NSString *)imgName
121+
- (UIImage *)imageNamed:(NSString *)key
122122
{
123-
return [self imageNamed:imgName forTheme:self.currentTheme];;
123+
return [self imageNamed:key forTheme:self.currentTheme];;
124124
}
125125

126-
- (UIImage *)imageNamed:(NSString *)imgName forTheme:(NSString*)themeName
126+
- (UIImage *)imageNamed:(NSString *)key forTheme:(NSString*)themeName
127127
{
128-
if ([self isValidString:themeName] == NO || [self isValidString:imgName] == NO) {
128+
if ([self isValidString:themeName] == NO || [self isValidString:key] == NO) {
129129
return nil;
130130
}
131131

132-
NSString *path = self.themeList[themeName];
133-
path = [self relativePathToMainBundle:path];
134-
NSString *filePath = [path stringByAppendingPathComponent:imgName];
135-
UIImage *img = [UIImage imageNamed:filePath];
132+
NSString *imgName = [self objectForKey:key forTheme:themeName];
133+
134+
if (imgName == nil) {
135+
imgName = key;
136+
}
137+
138+
UIImage* img = nil;
139+
NSBundle* bundle = [NSBundle bundleWithPath:self.themeList[themeName]];
140+
141+
if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_8_0) {
142+
img = [UIImage imageNamed:key inBundle:bundle compatibleWithTraitCollection:nil];
143+
}
144+
else {
145+
#ifdef AWLThemeManager_XCASSETS_iOS7
146+
//This is included for reference/completeness. It fetches the device specific
147+
//image from the compiled Assets.car embedded in the theme bundle for iOS7 devices.
148+
//However, it is a *PRIVATE API*
149+
//As such, all relevant warnings and caveats apply to it's usage
150+
//IF you want to enable with cocoapods you'll need this:
151+
//https://guides.cocoapods.org/syntax/podfile.html#post_install
152+
static NSString* iOS7PrivateCompatSelector = @"_" @"device" @"Specific" @"ImageNamed:" @"inBundle:";
153+
img = [UIImage performSelector:NSSelectorFromString(iOS7PrivateCompatSelector) withObject:imgName withObject:bundle];
154+
#endif
155+
}
156+
157+
if (img == nil) {
158+
NSString *path = self.themeList[themeName];
159+
path = [self relativePathToMainBundle:path];
160+
NSString *filePath = [path stringByAppendingPathComponent:imgName];
161+
img = [UIImage imageNamed:filePath];
162+
}
136163

137164
if (img == nil) {
138165
NSString *baseTheme = self.themeRelationship[themeName];

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,21 @@ If you want to use system font, you should ignore the first value, like this:
9494
bold,14 //boldSystemFontOfSize
9595
italic,14 //italicSystemFontOfSize
9696

97-
**Support reference** Same with the color.
97+
**Support reference**. Same with the color.
9898

99+
### Define image
100+
There are two way for adding img to theme bundle:
101+
102+
1. Put the image file in the bundle;
103+
2. Add a xcassets file , then put image files in the xcassets.
104+
105+
**Support reference**. Same with the color. If image name isn't found in defaults.plist, we use key as image name.
106+
107+
#### Other
99108
You can add whatever you want to the defaults.plist. Just use `objectForKey:` to get the value.
100109

110+
111+
101112
### How to use in your project
102113

103114
Add the absolute path of bundle to AWLThemeManager object, then set the current theme that you want.

ThemeManagerDemo/Assets/Info.plist

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>en</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIdentifier</key>
10+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
11+
<key>CFBundleInfoDictionaryVersion</key>
12+
<string>6.0</string>
13+
<key>CFBundleName</key>
14+
<string>$(PRODUCT_NAME)</string>
15+
<key>CFBundlePackageType</key>
16+
<string>BNDL</string>
17+
<key>CFBundleShortVersionString</key>
18+
<string>1.0</string>
19+
<key>CFBundleSignature</key>
20+
<string>????</string>
21+
<key>CFBundleVersion</key>
22+
<string>1</string>
23+
<key>NSHumanReadableCopyright</key>
24+
<string>Copyright © 2016年 AppWill. All rights reserved.</string>
25+
<key>NSPrincipalClass</key>
26+
<string></string>
27+
</dict>
28+
</plist>

0 commit comments

Comments
 (0)