补充~

补充下https://www.jianshu.com/p/2dece6f94abc~

例如,UIImage实例对象分类:
#import<UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN

@interfaceUIImage(theme)

@property(strong,nonatomic)NSString*imageName;

+ (UIImage *)imageWithKey:(NSString*)namecontainer:(id)view;

@end

NS_ASSUME_NONNULL_END

#import"UIImage+theme.h"

#import<objc/runtime.h>

#import"UIView+RefreshTheme.h"

#import"ThemeManager.h"

staticNSString*imageNameKey =@"imageNameKey";

@implementationUIImage(theme)

- (void)setImageName:(NSString*)imageName{


objc_setAssociatedObject(self, &imageNameKey,imageName,OBJC_ASSOCIATION_COPY);

}

- (NSString*)imageName{


returnobjc_getAssociatedObject(self, &imageNameKey);

}

+ (UIImage *)imageWithName:(NSString*)name{


UIImage *image = [UIImageimageNamed:[[ThemeManager getCurrentThemeImageData]objectForKey:name]];

if(!image) {


return[UIImageimageNamed:name];

    }

image.imageName= name;

returnimage;

}

+ (UIImage *)imageWithKey:(NSString*)namecontainer:(id)view{


UIImage *image = [UIImage imageNamed:[[ThemeManager getCurrentThemeImageData]objectForKey:name]];

if(!image) {


return[UIImageimageNamed:name];

    }

image.imageName= name;


    UIView *img = view;

img.imageName= name;


returnimage;

}

@end

例如UIView分类如下:

@implementationUIView(RefreshTheme)

- (void)didMoveToSuperview{


if(!isStartTheme) {


return;

    }


NSMutableArray*arr = [ThemeManager getThemeViews];


if(!arr) {


arr = [NSMutableArraynew];

    }

if(![arrcontainsObject:self]) {

//NSLog(@"1%@ ---- 2%@ ---- 3%@",self.colorName,self.textColorName,self.imageName);

[arraddObject:self];

//}

[ThemeManager setThemeViews:arr];

    }

}

- (void)traitCollectionDidChange:(nullable UITraitCollection *)previousTraitCollection{


if(!isStartTheme) {


return;

    }


if(@available(iOS13.0, *)) {


if([self.traitCollectionhasDifferentColorAppearanceComparedToTraitCollection:previousTraitCollection]) {


if(self.traitCollection.userInterfaceStyle== UIUserInterfaceStyleDark) {


NSLog(@暗黑模式");

[ThemeManager refreshTheme:@"ThemeType_DarkMode"];

}else{


NSLog(@" 普通模式");

[ThemeManager refreshTheme:@"ThemeType_Default"];

                }

            }

        }

}

@end


例如Thememanager主要做了如下工作:

#define Image(x,y)       [UIImage imageWithKey:x container:y] //图片

+ (NSDictionary*)getCurrentThemeImageData{


NSString*themeKey =  [[NSUserDefaultsstandardUserDefaults]objectForKey:ThemeKey];

if([themeKeyisEqualToString:ThemeDefault]) {


        themeKey = ThemeImageDefault;

}else{


        themeKey = ThemeImageDark;

    }

NSString*strPath            = [[NSBundlemainBundle]pathForResource:themeKeyofType:@"json"];

NSString*parseJason        = [[NSStringalloc]initWithContentsOfFile:strPathencoding:NSUTF8StringEncodingerror:nil];

NSData*data                = [parseJasondataUsingEncoding:NSUTF8StringEncoding];

idjsonObject                = [NSJSONSerializationJSONObjectWithData:dataoptions:NSJSONReadingMutableContainerserror:nil];

returnjsonObject;// 看具体协议结构

}

+ (void)refreshTheme:(NSString*)themeKey{


NSString*themeLOCAL = [[NSUserDefaultsstandardUserDefaults]objectForKey:ThemeKey];

if([themeLOCALisEqualToString:themeKey]) {


return;

    }


[[NSUserDefaultsstandardUserDefaults]setObject:themeKeyforKey:ThemeKey];

if(!themeViews) {


return;

    }


for(id container in themeViews) {


if([containerisKindOfClass:[UIButtonclass]]) {


            UIButton *btn = container;

//color

if(btn.colorName) {

btn.backgroundColor=BgColor(btn.colorName, btn);

            }

if(btn.textColorName) {

[btn setTitleColor:TextColor(btn.textColorName, btn)forState:UIControlStateNormal];

            }

//image

if(btn.currentImage.imageName) {

[btn setImage:Image(btn.imageView.image.imageName, btn)forState:UIControlStateNormal];

            }

if(btn.currentBackgroundImage.imageName) {


[btn setBackgroundImage:Image(btn.currentBackgroundImage.imageName, btn)forState:UIControlStateNormal];

            }

}elseif([containerisKindOfClass:[UIImageViewclass]]) {


            UIImageView *img = container;

if(img.image.imageName) {

img.image=Image(img.image.imageName, img);

            }

if(img.colorName) {


img.backgroundColor=BgColor(img.colorName, img);

            }

}//......未继续补充

    }

}

页面使用时,使用分类方法即可,例如:

img.image=Image(@"imageV", img);

还是轻量的很~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容