Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add intForKey, floatForKey, boolForKey methods for extra theming support #5

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion RNThemeManager.podspec
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
1 change: 1 addition & 0 deletions Source/RNThemeButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
16 changes: 14 additions & 2 deletions Source/RNThemeButton.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
2 changes: 2 additions & 0 deletions Source/RNThemeLabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
<RNThemeUpdateProtocol>

@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
20 changes: 18 additions & 2 deletions Source/RNThemeLabel.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions Source/RNThemeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
70 changes: 64 additions & 6 deletions Source/RNThemeManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
1 change: 1 addition & 0 deletions Source/RNThemeTextField.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<RNThemeUpdateProtocol>

@property (nonatomic, strong) NSString *fontKey;
@property (nonatomic ,strong) NSNumber *fontSize;
@property (nonatomic, strong) NSString *textColorKey;

@end
16 changes: 13 additions & 3 deletions Source/RNThemeTextField.m
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down
1 change: 1 addition & 0 deletions Source/RNThemeTextView.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<RNThemeUpdateProtocol>

@property (nonatomic, strong) NSString *fontKey;
@property (nonatomic, strong) NSNumber *fontSize;
@property (nonatomic, strong) NSString *textColorKey;

@end
14 changes: 12 additions & 2 deletions Source/RNThemeTextView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand Down