要运行示例项目,克隆仓库,然后首先从 Example 目录运行 pod install
。
StyleSheet 可通过 CocoaPods 获得。要安装,只需在您的 Podfile 中添加以下行:
pod "StyleSheet"
例如,要实现以下效果:两个label,一个button,一个view 基于原有的配置大概要写
self.label.layer.cornerRadius = 3;
self.label.textColor = [UIColor darkTextColor];
self.label.font = [UIFont systemFontOfSize:13];
self.label.backgroundColor = [UIColor greenColor];
self.label.layer.borderWidth = 2;
self.label.layer.borderColor = [UIColor redColor].CGColor;
self.label2.layer.cornerRadius = 3;
self.label2.textColor = [UIColor darkTextColor];
self.label2.font = [UIFont systemFontOfSize:13];
self.label2.backgroundColor = [UIColor greenColor];
self.label2.layer.borderWidth = 2;
self.label2.layer.borderColor = [UIColor redColor].CGColor;
self.button.layer.cornerRadius = 3;
self.button.backgroundColor = [UIColor greenColor];
self.button.layer.borderWidth = 2;
self.button.layer.borderColor = [UIColor redColor].CGColor;
self.aView.layer.cornerRadius = 3;
self.aView.backgroundColor = [UIColor greenColor];
self.aView.layer.borderWidth = 2;
self.aView.layer.borderColor = [UIColor redColor].CGColor;
所暴露的问题:
因此 StyleSheet 所要解决的问题就是:
目前阶段使用 StyleSheet 完成上述样式:
self.label.style = DZLabelStyleMake(
style.backgroundColor = [UIColor greenColor];
style.cornerRedius = 3;
style.borderColor = [UIColor redColor];
style.borderWidth = 2;
style.textStyle.textColor = [UIColor darkTextColor];
style.textStyle.font = [UIFont systemFontOfSize:13];
);
self.label2.style = self.label.style;
self.aView.style = self.label.style;
[self.button.style copyAttributesWithStyle:self.label.style];
在设计 StyleSheet 时有意淡化了对渲染 View 类型的概念,任何一种类型的 Style 可以渲染任何类型的 View,但是必须是这种类型的 View 支持这种 Style 所指称的属性。例如,您可以使用针对 Button 设计的 DZButtonStateStyle 来渲染一个 UILabel,但由于 UILabel 不支持 DZButtonStateStyle 中的渲染属性,所以渲染结果是无效的。
但是当使用 DZButtonStyle(继承自 DZViewStyle)来渲染 UILabel 时,会使用 DZButtonStyle 中其父类的某些渲染属性来渲染 UILabel 的父类 UIView 所支持的属性。
DZLabelStyle* style = DZLabelStyleMake(
style.backgroundColor = [UIColor greenColor];
style.cornerRedius = 3;
style.borderColor = [UIColor redColor];
style.borderWidth = 2;
style.textStyle.textColor = [UIColor darkTextColor];
style.textStyle.font = [UIFont systemFontOfSize:13];
);
[style decorateView:self.label];
直接渲染的好处是,不需要再次生成 Style 对象,更方便样式在多个 View 之间渲染。
self.label.style = style;
或者
self.label.style = DZLabelStyleMake(
style.backgroundColor = [UIColor greenColor];
style.cornerRedius = 3;
style.borderColor = [UIColor redColor];
style.borderWidth = 2;
style.textStyle.textColor = [UIColor darkTextColor];
style.textStyle.font = [UIFont systemFontOfSize:13];
);
在赋值渲染时,会将 Style 的副本实例与当前 View 绑定,当更改 Style 的属性时,对应 View 的样式会立即改变。
使用原始配置,进行通用样式的共享是个非常困难的事情,基本上都是体力活,靠人力来维护。我们的代码中会掺杂大量用于配置样式的代码,而且是独立且散在的。
现在你可以通过 StyleSheet 解决:
EXTERN_SHARE_LABEL_STYLE(Content)
IMP_SHARE_LABEL_STYLE(Content,
style.backgroundColor = [UIColor clearColor];
style.cornerRedius = 2;
style.textStyle.textColor = [UIColor redColor];
)
self.label.style = DZStyleContent();
很多时候,如果不需要进一步更改样式,可以不采用复制赋值的方式进行渲染,可以直接使用:
[DZStyleContent() decorateView:self.label];
仅进行渲染,不进行复制
样式表在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。