Created
January 29, 2015 03:20
-
-
Save shiweifu/cb066ad0c9369c6f0559 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Created by shiweifu on 1/21/15. | |
// Copyright (c) 2015 SF. All rights reserved. | |
// | |
#import "SFResourceManager.h" | |
#import "HexColor.h" | |
@interface SFResourceManager() | |
@property (nonatomic, strong) NSArray *fontResourceObject; | |
@property (nonatomic, strong) NSArray *colorResourceObject; | |
@property (nonatomic, strong) NSArray *backgroundColorResourceObject; | |
@end | |
@implementation SFResourceManager | |
{ | |
} | |
- (instancetype)init | |
{ | |
self = [super init]; | |
if (self) | |
{ | |
NSString *path = [[NSBundle mainBundle] pathForResource:@"res" ofType:@"json"]; | |
NSError *error = nil; | |
NSData *data = [[NSData alloc] initWithContentsOfFile:path]; | |
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data | |
options:NSJSONReadingAllowFragments | |
error:&error]; | |
self.fontResourceObject = jsonObject[@"font"]; | |
self.colorResourceObject = jsonObject[@"color"]; | |
self.backgroundColorResourceObject = jsonObject[@"backgroundColor"]; | |
} | |
return self; | |
} | |
+ (UIFont *)fontWithAttrName:(NSString *)attrName | |
{ | |
UIFont *f = nil; | |
for(NSDictionary *d in [self.instance fontResourceObject]) | |
{ | |
if([d[@"name"] isEqualToString:attrName]) | |
{ | |
NSNumber *fontSize = d[@"font_size"]; | |
NSString *fontName = d[@"font_name"]; | |
f = [UIFont fontWithName:fontName size:fontSize.integerValue]; | |
break; | |
} | |
} | |
return f; | |
} | |
+ (UIColor *)colorWithAttrName:(NSString *)attrName | |
{ | |
UIColor *c = nil; | |
for(NSDictionary *d in [self.instance colorResourceObject]) | |
{ | |
if ([d[@"name"] isEqualToString:attrName]) | |
{ | |
c = [UIColor colorWithHexString:d[@"value"]]; | |
break; | |
} | |
} | |
return c; | |
} | |
+ (UIColor *)backgroundColorWithAttrName:(NSString *)attrName | |
{ | |
UIColor *c = nil; | |
for(NSDictionary *d in [self.instance backgroundColorResourceObject]) | |
{ | |
if ([d[@"name"] isEqualToString:attrName]) | |
{ | |
c = [UIColor colorWithHexString:d[@"value"]]; | |
break; | |
} | |
} | |
return c; | |
} | |
+ (SFResourceManager *)instance | |
{ | |
static SFResourceManager *_instance = nil; | |
@synchronized (self) | |
{ | |
if (_instance == nil) | |
{ | |
_instance = [[self alloc] init]; | |
} | |
} | |
return _instance; | |
} | |
@end | |
@implementation UIView(ResourceManager) | |
- (void)setResourceWithAttrName:(NSString *)attrName | |
{ | |
UIColor *textColor = [SFResourceManager colorWithAttrName:attrName]; | |
UIColor *backgroundColor = [SFResourceManager backgroundColorWithAttrName:attrName]; | |
UIFont *font = [SFResourceManager fontWithAttrName:attrName]; | |
backgroundColor = backgroundColor?backgroundColor: [UIColor clearColor]; | |
if([self isKindOfClass:UIButton.class] ) | |
{ | |
UIButton *btn = (UIButton *) self; | |
[btn setBackgroundColor:backgroundColor]; | |
[btn.titleLabel setFont:font]; | |
[btn setTitleColor:textColor forState:UIControlStateNormal]; | |
} | |
else if([self isKindOfClass:UILabel.class]) | |
{ | |
UILabel *label = (UILabel *)self; | |
[label setFont:font]; | |
[label setBackgroundColor:backgroundColor]; | |
[label setTextColor:textColor]; | |
} | |
else if([self isKindOfClass:UIView.class]) | |
{ | |
UIView *v = self; | |
[v setBackgroundColor:backgroundColor]; | |
} | |
} | |
@end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment