iOS app风格设置启发
之前看过一篇文章,讲述的是通过文件方式控制控制器的跳转及风格,例如很多app都会有设置页面,当然这个控制器会存在一个UITableView,或许你会给cell创建模型,模型中包含图片的路径(or名字),cell的高度,cell的title等等一系列属性,但是这些同样可以存储在plist文件中,具体的大家可以自己慢慢yy,当然这种方法仁者见仁智者见智^_^。
由此博主心中存在这样一个问题,按照这样的思路把对应的app风格(颜色,字体等)的设置是否也能事先写在plist文件?或许你在写项目的时候会像博主一样,写一个BZCommonStyle的类负责app的风格设置,又或者会写N多的宏定义。。。不多说,直接切入正题,本文只以颜色举例。
1.首先创建plist文件此处命名为StyleConfig.plist,输入数据:
2.创建配置类:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
static NSString * const kColor_Key = @"CommonColor"; ///获取颜色配置的key
static NSString * const kColor_Background = @"Background"; /// 颜色-背景颜色
static NSString * const kColor_Blue_Light = @"A"; /// 颜色-粉色
static NSString * const kColor_Black = @"B"; /// 颜色-半透明黑色
@interface BZCommonConfig : NSObject
+ (instancetype)instance;
- (UIColor *)colorForKey:(NSString *)key;
@end
#import "BZCommonConfig.h"
@interface BZCommonConfig ()
@property (nonatomic, strong) NSDictionary *configMap;
@end
@implementation BZCommonConfig
+ (instancetype)instance{
static BZCommonConfig *obj = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
obj = [[BZCommonConfig alloc] init];
});
return obj;
}
- (instancetype)init{
self = [super init];
if (self) {
NSString *configPath = [[NSBundle mainBundle] pathForResource:@"StyleConfig" ofType:@"plist"];
self.configMap = [NSDictionary dictionaryWithContentsOfFile:configPath];
}
return self;
}
- (UIColor *)colorForKey:(NSString *)key{
//得到所有颜色的字典
NSDictionary *colorMap = self.configMap[kColor_Key];
//得到颜色的key
NSString *colorKey = [@"color" stringByAppendingString:key];
NSString *colorString = colorMap[colorKey][@"rgb"];
//得到对应的rgba值
CGFloat redValue;
CGFloat greenValue;
CGFloat blueValue;
CGFloat alphaValue;
NSArray *colorArray = [colorString componentsSeparatedByString:@","];
if (colorArray) {
redValue = [[colorArray objectAtIndex:0] floatValue];
greenValue = [[colorArray objectAtIndex:1] floatValue];
blueValue = [[colorArray objectAtIndex:2] floatValue];
if (colorArray.count == 3) {
alphaValue = 1.0f;
}else {
alphaValue = [[colorArray objectAtIndex:3] floatValue];
}
} else {
return nil;
}
//如果透明度小于等于0,返回clearColor
if (alphaValue <= 0.0f) return [UIColor clearColor];
//返回所对应的key
return [UIColor colorWithRed:redValue / 255.0f
green:greenValue / 255.0f
blue:blueValue / 255.0
alpha:alphaValue];
}
@end
3.控制器实现:
- (void)viewDidLoad {
[super viewDidLoad];
[self _initUI];
}
#pragma mark - init
- (void)_initUI{
self.view.backgroundColor = [[BZCommonConfig instance] colorForKey:kColor_Background];
[self labelA];
[self labelB];
}
#pragma mark - property
- (UILabel *)labelA{
if (!_labelA) {
_labelA = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, self.view.bounds.size.width - 20 * 2, 130)];
_labelA.backgroundColor = [[BZCommonConfig instance] colorForKey:kColor_Blue_Light];
[self.view addSubview:_labelA];
}
return _labelA;
}
- (UILabel *)labelB{
if (!_labelB) {
_labelB = [[UILabel alloc] initWithFrame:CGRectMake(20, CGRectGetMaxY(_labelA.frame) + 30, self.view.bounds.size.width - 20 * 2, 130)];
_labelB.backgroundColor = [[BZCommonConfig instance] colorForKey:kColor_Black];
[self.view addSubview:_labelB];
}
return _labelB;
}
4.最终效果:
6.总结:代码其实非常非常简单,这样做或许在项目迭代更新的时候会非常方便,直接替换plist文件即可。这种方法与直接在类里进行相关配置各有优势,博主个人还是比较喜欢用文件统一管理,其实各个控制器的颜色,字体等属性均可写在里面,便于统一管理。