前言
看過CSS的設置方法知道碱工,把要設置的對象提取了出來娃承,再在括號里設置。有了模仿對象怕篷,就開始模仿了历筝!
在RAC強大的宏定義下,在其基礎上做了層封裝廊谓,實現(xiàn)類似的效果梳猪。
主要代碼
#define ml_css(OBJ, ...) ml_css_(OBJ, __VA_ARGS__)
#define ml_css_(OBJ, ...) metamacro_concat(ml_css_, metamacro_argcount(__VA_ARGS__))(0, OBJ, __VA_ARGS__)
#define ml_css_0(INDEX, OBJ, ...)
#define ml_css_1(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_0(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_2(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_1(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_3(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_2(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_4(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_3(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_5(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_4(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_6(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_5(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_7(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_6(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_8(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_7(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_9(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_8(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_10(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_9(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_11(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_10(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_12(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_11(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_13(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_12(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_14(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_13(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_15(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_14(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_16(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_15(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_17(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_16(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_18(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_17(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_19(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_18(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
#define ml_css_20(INDEX, OBJ, ...) OBJ.metamacro_at(INDEX, __VA_ARGS__);\
ml_css_19(metamacro_inc(INDEX), OBJ, __VA_ARGS__)
這段代碼是在RAC宏定義的基礎上的封裝的宏定義,最多支持設置20個屬性(20夠多了蒸痹,有需要可自行增加)春弥。
使用
#define ml_css(OBJ, ...) ml_css_(OBJ, __VA_ARGS__)
OBJ為要設置的對象,其余不定參數(shù)為對該對象的設置
示例如下:
UIView *view = [[UIView alloc] init];
[self.view addSubview:view];
ml_css(view,
frame = CGRectMake(100, 100, 200, 200),
backgroundColor = [UIColor redColor],
layer.borderColor = [UIColor blueColor].CGColor,
layer.borderWidth = 30,
layer.cornerRadius = 75,
clipsToBounds = YES)
設置成功!
Tips:如何查看到編譯后的宏定義
可以看到上述代碼如下,除了沒有空格與換行叠荠,與普通方法設置沒有差別匿沛。
view.frame = CGRectMake(100, 100, 200, 200);view.backgroundColor = [UIColor redColor];view.layer.borderColor = [UIColor blueColor].CGColor;view.layer.borderWidth = 30;view.layer.cornerRadius = 75;view.clipsToBounds = __objc_yes;