StyleSheet 0.1.5

StyleSheet 0.1.5

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2016年2月

ImageView. 维护。



  • 作者:
  • dongzhao

使用方法

要运行示例项目,克隆仓库,然后首先从 Example 目录运行 pod install

要求

  1. UIKit

安装

StyleSheet 可通过 CocoaPods 获得。要安装,只需在您的 Podfile 中添加以下行:

pod "StyleSheet"

原始方法有什么问题?

例如,要实现以下效果:两个label,一个button,一个view image 基于原有的配置大概要写

    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;

所暴露的问题:

  1. 繁琐的代码,大量重复性的工作
  2. 样式无法共享,每个 View 都需要重新进行样式赋值。

因此 StyleSheet 所要解决的问题就是:

  1. 样式配置轻便化,能够使用更少的代码来描述 View 的样式
  2. 样式在 View 之间的共享

目前阶段使用 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 所支持的属性。

直接使用 Style 对 View 进行渲染:

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];

仅进行渲染,不进行复制

支持的类型

  1. UIView
  2. UILabel
  3. UITextView
  4. UITextField
  5. UIButton

计划中支持的类型

  1. UISearchBar
  2. UINavigationBar
  3. .....

作者

[email protected]

许可证

样式表在MIT许可证下可用。有关更多信息,请参阅LICENSE文件。