diff --git a/RNThemeManager.podspec b/RNThemeManager.podspec index 9b7f588..7b956a0 100644 --- a/RNThemeManager.podspec +++ b/RNThemeManager.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "RNThemeManager" - s.version = "0.1.0" + s.version = "0.2.0" s.summary = "Easily manage themes and respond to theme changes by updating views in real time." s.homepage = "https://github.com/rnystrom/RNThemeManager" s.license = 'MIT' diff --git a/Source/RNThemeButton.h b/Source/RNThemeButton.h index dd6e1ea..a9a5827 100644 --- a/Source/RNThemeButton.h +++ b/Source/RNThemeButton.h @@ -15,6 +15,7 @@ @property (nonatomic, strong) NSString *backgroundImageKey; @property (nonatomic, strong) NSString *backgroundColorKey; @property (nonatomic, strong) NSString *fontKey; +@property (nonatomic, strong) NSNumber *fontSize; @property (nonatomic, strong) NSString *textColorKey; @property (nonatomic, strong) NSString *highlightedTextColorKey; diff --git a/Source/RNThemeButton.m b/Source/RNThemeButton.m index 8105027..6cd964f 100644 --- a/Source/RNThemeButton.m +++ b/Source/RNThemeButton.m @@ -53,9 +53,21 @@ - (void)dealloc { - (void)applyTheme { UIFont *font = nil; - if (self.fontKey && (font = [[RNThemeManager sharedManager] fontForKey:self.fontKey])) { - self.titleLabel.font = font; + + if (self.fontKey) { + // has the fontSize override been provided? + if (self.fontSize) { + font = [[RNThemeManager sharedManager] fontForKey:self.fontKey size:[self.fontSize floatValue]]; + } + else { + font = [[RNThemeManager sharedManager] fontForKey:self.fontKey]; + } + + if (font) { + self.titleLabel.font = font; + } } + UIColor *textColor = nil; if (self.textColorKey && (textColor = [[RNThemeManager sharedManager] colorForKey:self.textColorKey])) { [self setTitleColor:textColor forState:UIControlStateNormal]; diff --git a/Source/RNThemeLabel.h b/Source/RNThemeLabel.h index 82bcd4e..428dbd6 100644 --- a/Source/RNThemeLabel.h +++ b/Source/RNThemeLabel.h @@ -13,7 +13,9 @@ @property (nonatomic, strong) NSString *fontKey; +@property (nonatomic, strong) NSNumber *fontSize; @property (nonatomic, strong) NSString *textColorKey; +@property (nonatomic, strong) NSString *highlightedTextColorKey; @property (nonatomic, strong) NSString *backgroundColorKey; @end diff --git a/Source/RNThemeLabel.m b/Source/RNThemeLabel.m index e34d0da..5c84373 100644 --- a/Source/RNThemeLabel.m +++ b/Source/RNThemeLabel.m @@ -53,13 +53,29 @@ - (void)dealloc { - (void)applyTheme { UIFont *font = nil; - if (self.fontKey && (font = [[RNThemeManager sharedManager] fontForKey:self.fontKey])) { - self.font = font; + + if (self.fontKey) { + // has the fontSize override been provided? + if (self.fontSize) { + font = [[RNThemeManager sharedManager] fontForKey:self.fontKey size:[self.fontSize floatValue]]; + } + else { + font = [[RNThemeManager sharedManager] fontForKey:self.fontKey]; + } + + if (font) { + self.font = font; + } } + UIColor *textColor = nil; if (self.textColorKey && (textColor = [[RNThemeManager sharedManager] colorForKey:self.textColorKey])) { self.textColor = textColor; } + UIColor *highlightedTextColor = nil; + if (self.highlightedTextColorKey && (highlightedTextColor = [[RNThemeManager sharedManager] colorForKey:self.highlightedTextColorKey])) { + self.highlightedTextColor = highlightedTextColor; + } UIColor *backgroundColor = nil; if (self.backgroundColorKey && (backgroundColor = [[RNThemeManager sharedManager] colorForKey:self.backgroundColorKey])) { self.backgroundColor = backgroundColor; diff --git a/Source/RNThemeManager.h b/Source/RNThemeManager.h index 2d334eb..14a7cd8 100644 --- a/Source/RNThemeManager.h +++ b/Source/RNThemeManager.h @@ -23,14 +23,27 @@ extern NSString * const RNThemeManagerDidChangeThemes; // Requires 2 keys per font entry. The first key's name doesn't matter, but the size key must be suffixed with "Size" // ie: @{ @"labelFont" : @"Helvetica", @"labelFontSize" : @15 } +// If the size key is not specified, the system default will be assumed - (UIFont *)fontForKey:(NSString*)key; +// Allows the font size to be provided separately rather than using the themed style +- (UIFont *)fontForKey:(NSString *)key size:(CGFloat)size; + // Return a UIColor from a hex color stored in theme file - (UIColor *)colorForKey:(NSString *)key; // Return a UIImage for an image name stored in theme file - (UIImage *)imageForKey:(NSString *)key; +// Return an int stored in a theme file, using a default value if not found +- (int)intForKey:(NSString *)key defaultValue:(int)defaultValue; + +// Return an float stored in a theme file, using a default value if not found +- (float)floatForKey:(NSString *)key defaultValue:(float)defaultValue; + +// Return an bool stored in a theme file, using a default value if not found +- (BOOL)boolForKey:(NSString *)key defaultValue:(BOOL)defaultValue; + // Change the theme name, should not include .plist extension - (void)changeTheme:(NSString *)themeName; diff --git a/Source/RNThemeManager.m b/Source/RNThemeManager.m index 0d25c39..1d79ac0 100644 --- a/Source/RNThemeManager.m +++ b/Source/RNThemeManager.m @@ -100,20 +100,31 @@ - (NSDictionary *)inheritedThemeWithParentTheme:(NSString *)parentThemeName chil - (UIFont *)fontForKey:(NSString*)key { NSString *sizeKey = [key stringByAppendingString:@"Size"]; - NSString *fontName = self.styles[key]; - NSString *size = self.styles[sizeKey]; + + // get the font size, using default if not supplied + CGFloat size = [self floatForKey:sizeKey defaultValue:[UIFont systemFontSize]]; while (self.styles[fontName]) { fontName = self.styles[fontName]; } - while (self.styles[size]) { - size = self.styles[size]; + if (fontName) { + return [UIFont fontWithName:fontName size:size]; + } + return nil; +} + +- (UIFont *)fontForKey:(NSString *)key size:(CGFloat)size +{ + NSString *fontName = self.styles[key]; + + while (self.styles[fontName]) { + fontName = self.styles[fontName]; } - if (fontName && size) { - return [UIFont fontWithName:fontName size:size.floatValue]; + if (fontName) { + return [UIFont fontWithName:fontName size:size]; } return nil; } @@ -148,4 +159,51 @@ - (UIImage *)imageForKey:(NSString *)key { return nil; } +#pragma mark - Number + +- (int)intForKey:(NSString *)key defaultValue:(int)defaultValue +{ + NSString *num = self.styles[key]; + + while (self.styles[num]) { + num = self.styles[num]; + } + + if (num) { + return num.intValue; + } + + return defaultValue; +} + +- (float)floatForKey:(NSString *)key defaultValue:(float)defaultValue +{ + NSString *num = self.styles[key]; + + while (self.styles[num]) { + num = self.styles[num]; + } + + if (num) { + return num.floatValue; + } + + return defaultValue; +} + +- (BOOL)boolForKey:(NSString *)key defaultValue:(BOOL)defaultValue +{ + NSString *num = self.styles[key]; + + while (self.styles[num]) { + num = self.styles[num]; + } + + if (num) { + return num.boolValue; + } + + return defaultValue; +} + @end diff --git a/Source/RNThemeTextField.h b/Source/RNThemeTextField.h index 1ebb3b3..a59ff74 100644 --- a/Source/RNThemeTextField.h +++ b/Source/RNThemeTextField.h @@ -13,6 +13,7 @@ @property (nonatomic, strong) NSString *fontKey; +@property (nonatomic ,strong) NSNumber *fontSize; @property (nonatomic, strong) NSString *textColorKey; @end diff --git a/Source/RNThemeTextField.m b/Source/RNThemeTextField.m index d613a1e..25b08b3 100644 --- a/Source/RNThemeTextField.m +++ b/Source/RNThemeTextField.m @@ -52,9 +52,19 @@ - (void)dealloc { } - (void)applyTheme { - UIFont *font = nil; - if (self.fontKey && (font = [[RNThemeManager sharedManager] fontForKey:self.fontKey])) { - self.font = font; + UIFont *font = nil; + if (self.fontKey) { + // has the fontSize override been provided? + if (self.fontSize) { + font = [[RNThemeManager sharedManager] fontForKey:self.fontKey size:[self.fontSize floatValue]]; + } + else { + font = [[RNThemeManager sharedManager] fontForKey:self.fontKey]; + } + + if (font) { + self.font = font; + } } UIColor *textColor = nil; if (self.textColorKey && (textColor = [[RNThemeManager sharedManager] colorForKey:self.textColorKey])) { diff --git a/Source/RNThemeTextView.h b/Source/RNThemeTextView.h index 3ecd234..ccc463b 100644 --- a/Source/RNThemeTextView.h +++ b/Source/RNThemeTextView.h @@ -13,6 +13,7 @@ @property (nonatomic, strong) NSString *fontKey; +@property (nonatomic, strong) NSNumber *fontSize; @property (nonatomic, strong) NSString *textColorKey; @end diff --git a/Source/RNThemeTextView.m b/Source/RNThemeTextView.m index 2b6b511..c5e5b97 100644 --- a/Source/RNThemeTextView.m +++ b/Source/RNThemeTextView.m @@ -53,8 +53,18 @@ - (void)dealloc { - (void)applyTheme { UIFont *font = nil; - if (self.fontKey && (font = [[RNThemeManager sharedManager] fontForKey:self.fontKey])) { - self.font = font; + if (self.fontKey) { + // has the fontSize override been provided? + if (self.fontSize) { + font = [[RNThemeManager sharedManager] fontForKey:self.fontKey size:[self.fontSize floatValue]]; + } + else { + font = [[RNThemeManager sharedManager] fontForKey:self.fontKey]; + } + + if (font) { + self.font = font; + } } UIColor *textColor = nil; if (self.textColorKey && (textColor = [[RNThemeManager sharedManager] colorForKey:self.textColorKey])) {