#import "UIView+EATheme.h"
#import "EAThemeManager.h"
#import
static void *EAViewThemesKey = "EAViewThemesKey";
static void *EAViewThemesName = "EAViewThemesName";
@interface UIView ()
@property (nonatomic, copy) NSString *viewThemesName;
@property (nonatomic, assign) ThemeViewType themesViewType;
@end
@implementation UIView (EATheme)
+ (void)load{
staticdispatch_once_tonceToken;
dispatch_once(&onceToken, ^{
Method deallocMethod = class_getInstanceMethod([self class], NSSelectorFromString(@"dealloc"));
Methodea_deallocMethod =class_getInstanceMethod([selfclass],@selector(ea_dealloc));
method_exchangeImplementations(deallocMethod, ea_deallocMethod);
});
}
- (void)setThemeContents:(THEME_CONTENTS)themeContents {
objc_setAssociatedObject(self, EAViewThemesKey, themeContents, OBJC_ASSOCIATION_COPY);
}
- (THEME_CONTENTS)themeContents {
return objc_getAssociatedObject(self, EAViewThemesKey);
}
- (void)ea_setThemeContents:(THEME_CONTENTS)themeContents {
if(themeContents) {
self.themeContents= themeContents;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(p_changeTheme) name:kEAChangeThemeNotification object:nil];
[self p_changeTheme];
}
}
- (void)p_changeTheme {
NSString*contentThemeIdentifier = [[EAThemeManagershareManager]currentThemeIdentifier];
if(contentThemeIdentifier) {
if(self.themeContents) {
self.themeContents(self, contentThemeIdentifier);
}
if(self.viewThemesName) {
[self changeTheme:contentThemeIdentifier iconName:self.viewThemesName themeViewType:self.themesViewType];
}
}
}
#pragma 新增方法
-(void)addIconName:(NSString*)iconNamethemeViewType:(ThemeViewType)themeViewType{
NSString*currentThemeIdentifier = [[EAThemeManagershareManager]currentThemeIdentifier];
self.viewThemesName=iconName;
self.themesViewType=themeViewType;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(p_changeTheme) name:kEAChangeThemeNotification object:nil];
[selfchangeTheme:currentThemeIdentifiericonName:iconNamethemeViewType:themeViewType];
}
-(void)changeTheme:(NSString*)currentThemeIdentifiericonName:(NSString*)iconNamethemeViewType:(ThemeViewType)themeViewType{
//themeViewType 21表示设置button的image,22设置bgimage
if(!iconName) {
iconName =@"0";
}
if ([self isKindOfClass:[UIImageView class]]) {
UIImageView*imgView = (UIImageView*)self;
NSString*imageName = [NSStringstringWithFormat:@"%@%@",iconName,currentThemeIdentifier];
imgView.image= [UIImageimageNamed:imageName];
}else if([self isKindOfClass:[UILabel class]]){
UILabel*label = (UILabel*)self;
[labelsetTextColor:[UIColorcolorWithHexString:[CommonFunctioncurrentThemeColorByIdentity:currentThemeIdentifier]]];
}
else if([self isKindOfClass:[UIButton class]]){
UIButton*btn = (UIButton*)self;
NSString*imageName = [NSStringstringWithFormat:@"%@%@", iconName,currentThemeIdentifier];
UIImage*image=[UIImageimageNamed:imageName];
if(image) {
if(themeViewType ==ThemeTypeButtonImageNormal) {
[btnsetImage:image forState:UIControlStateNormal];
}elseif(themeViewType ==ThemeTypeButtonImageSelect) {
[btnsetImage:image forState:UIControlStateSelected];
}else{
[btnsetBackgroundImage:image forState:UIControlStateNormal];
}
}elseif(ThemeTypeButtonLabelColor== themeViewType){
[btnsetTitleColor:[UIColorcolorWithHexString:[CommonFunctioncurrentThemeColorByIdentity:currentThemeIdentifier]]forState:UIControlStateNormal];
}else{
btn.backgroundColor=[UIColorcolorWithHexString:[CommonFunctioncurrentThemeColorByIdentity:currentThemeIdentifier]];
}
}else{
self.layer.shadowColor= [UIColorcolorWithHexString:[CommonFunctioncurrentThemeColorByIdentity:currentThemeIdentifier]].CGColor;
self.backgroundColor = [UIColor colorWithHexString:[CommonFunction currentThemeColorByIdentity:currentThemeIdentifier]]; }
}
#pragma 添加属性
- (NSInteger)themesViewType{
return [objc_getAssociatedObject(self,@selector(themesViewType)) integerValue];
}
- (void)setThemesViewType:(NSInteger)themesViewType{
objc_setAssociatedObject(self,@selector(themesViewType),@(themesViewType),OBJC_ASSOCIATION_ASSIGN);
}
- (NSString *)viewThemesName {
return objc_getAssociatedObject(self, EAViewThemesName);
}
- (void)setViewThemesName:(NSString*)viewThemesName {
objc_setAssociatedObject(self, EAViewThemesName, viewThemesName, OBJC_ASSOCIATION_COPY);
}