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文件即可。这种方法与直接在类里进行相关配置各有优势,博主个人还是比较喜欢用文件统一管理,其实各个控制器的颜色,字体等属性均可写在里面,便于统一管理。

转载于:https://www.cnblogs.com/coderYe/p/5357297.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值