回来了 ,还是用这个吧
暗黑模式
#define iSDark ([UIApplication sharedApplication].delegate.window.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) //是否是暗黑模式
if (@available(iOS 13.0, *)) {
UIColor *divideLineDyColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull trainCollection) {
if ([trainCollection userInterfaceStyle] == UIUserInterfaceStyleLight) {
return [UIColor colorWithRed:rgb2 green:rgb2 blue:rgb2 alpha:1.0];
} else {
CGFloat lineDarkRgb = 100 / 255.0;
return [UIColor colorWithRed:lineDarkRgb green:lineDarkRgb blue:lineDarkRgb alpha:1.0];
}
}];
_divideLine.backgroundColor = divideLineDyColor;
} else {
_divideLine.backgroundColor = [UIColor colorWithRed:rgb2 green:rgb2 blue:rgb2 alpha:1.0];
}
- (void)configCurrentThemeWithThemeName:(NSString *)themeName {
if (!themeName || [themeName isEqualToString:@""] || [themeName isKindOfClass:[NSNull class]]) {
return;
}
// NSString *jsonFileName = [_themeListDic objectForKey:themeName];
NSString *jsonFileName = [_themeListDic valueForKey:themeName];
if (!jsonFileName || [jsonFileName isEqualToString:@""] || [jsonFileName isKindOfClass:[NSNull class]]) {
[[NSUserDefaults standardUserDefaults] setObject:_themeName forKey:@"ThemeName"];
return;
}
if (@available(iOS 12.0, *)) {
BOOL isDark = ([UIApplication sharedApplication].delegate.window.traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark);
if (isDark && ([_themeName isEqualToString:@"TerryKou"] || [_themeName isEqualToString:@"TPP"])) {
jsonFileName = [NSString stringWithFormat:@"%@_dark",jsonFileName];
}
if (isDark) {
_keyboardThemeFileName = @"keyboard_dark";
}
} else {
}
NSString *fileName = [NSString stringWithFormat:@"%@.json",jsonFileName];
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName ofType:nil]];
NSData *keyboardData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@.json", _keyboardThemeFileName] ofType:nil]];
if (data == nil || keyboardData == nil) {
return;
}
NSDictionary *keyboardDic = [NSJSONSerialization JSONObjectWithData:keyboardData options:NSJSONReadingAllowFragments error:nil];
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
ThemePropertys *keyboardThemePropertys = [ThemePropertys yy_modelWithDictionary:keyboardDic];
ThemePropertys *themePropertys = [ThemePropertys yy_modelWithDictionary:dic];
[keyboardThemePropertys setKeyboardPropertys];
[themePropertys setPropertys];
}
定义一个颜色类
- (void)setPropertys{
TCOLOR_THEME_navigation = _TCOLOR_THEME_navigation;
}
把json文件通过YYModel赋值给ThemePropertys模型, 通过模型类 方法setPropertys 赋值给外部(extern)全局变量 TCOLOR_THEME_navigation
——————————————————————————————————
Theme.h.m
定义外部全局变量
extern NSString* TCOLOR_THEME_version;
Theme.m
NSString* TCOLOR_THEME_version;
定义全局变量NSString* TCOLOR_version=@"";
用外部全局变量给全局变量赋值
+(void)setThemeComponentStyle{
TCOLOR_version = TCOLOR_THEME_version;
}
ThemeProperty 是模型类
属性@property (nonatomic, copy, readwrite) NSString *TCOLOR_THEME_version;
- (void)setPropertys{
全局变量TCOLOR_THEME_version = 属性_TCOLOR_THEME_version;
}
Json: “TCOLOR_THEME_version” : “efefef”,
代码中
[Btncancle setTitleColor:[CommonMethods colorWithHexColorString:TCOLOR_version] forState:UIControlStateNormal];
TCOLOR_THEME_version; = 13123
1 Theme.m中的方法调用 , 把json 转化为模型
- (void)configCurrentThemeWithThemeName:(NSString *)themeName {}
模型中属性得到值 调用- (void)setPropertys方法 赋值给 外部全局变量TCOLOR_THEME_version
再调用Theme.m+(void)setThemeComponentStyle{ 赋值给全局变量TCOLOR_version
—————————————————————————————————
CommonMethods
+(UIColor *)colorWithHexColorString:(NSString *)hexColorString{
if ([hexColorString length]<6)
{
return [ UIColor blackColor];
}
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *tempString = [hexColorString lowercaseString];
if ([tempString hasPrefix:@"0x"]) { //检查开头是0x
tempString = [tempString substringFromIndex:2];
}
else if ([tempString hasPrefix:@"#"]){ //检查开头是#
tempString = [tempString substringFromIndex:1];
}
if ([tempString length] != 6) {
return [UIColor blackColor];
}
//分解三种颜色的值
NSRange range;
range.location=0;
range.length=2;
NSString *rString = [tempString substringWithRange:range];
range.location=2;
NSString *gString = [tempString substringWithRange:range];
range.location=4;
NSString *bString = [tempString substringWithRange:range];
//取三种颜色值
unsigned int red,green,blue;
[[NSScanner scannerWithString:rString] scanHexInt:&red];
[[NSScanner scannerWithString:gString] scanHexInt:&green];
[[NSScanner scannerWithString:bString] scanHexInt:&blue];
[pool drain];
return [UIColor colorWithRed:((float)red/255.0f)
green:((float)green/255.0f)
blue:((float)blue/255.0f)
alpha:100.0f];
}
——————————————————————————————————
stackView
@IBAction func addStar(_ sender: Any) {
let starImgVw:UIImageView = UIImageView(image: UIImage(named: "star"))
starImgVw.contentMode = .scaleAspectFit
self.horizontalStackView.addArrangedSubview(starImgVw)
UIView.animate(withDuration: 0.25, animations: {
self.horizontalStackView.layoutIfNeeded()
})
}
@IBAction func removeStar(_ sender: Any) {
let star:UIView? = self.horizontalStackView.arrangedSubviews.last
if let aStar = star
{
self.horizontalStackView.removeArrangedSubview(aStar)
aStar.removeFromSuperview()
UIView.animate(withDuration: 0.25, animations: {
self.horizontalStackView.layoutIfNeeded()
})
}
}