SKStyleKit
SKStyleKit 是一个简单的视觉组件样式库。用 Swift 编写,既支持 Swift 也支持 Objective C。
功能
- 一旦声明了样式,就可以在您的应用程序中使用它。
- 使用 StyleKit,您可以仅修改样式声明,而无需再编辑 xibs、storyboards 或代码。
- 样式以易于阅读和编写的 JSON 格式声明。
安装
CocoaPods
要使用 CocoaPods 将 SKStyleKit 集成到您的 Xcode 项目中,在您的 Podfile 中指定它
pod 'SKStyleKit'
操作手册
克隆仓库并将文件从 源文件
文件夹拖入您的 Xcode 项目。
入门指南
在首次调用样式工具包之前,应进行初始化。建议在您的应用程序代理中进行此操作
import SKStyleKit
class AppDelegate: UIResponder, UIApplicationDelegate {
<...>
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
StyleKit.initStyleKit()
return true
}
<...>
}
样式声明
样式以 JSON 格式声明,将 JSON 文件添加到您的项目中,命名为 style.json
,并声明第一个样式
{
"labelStyle": {
"fontSize": 15,
"fontColor": "#7F007F",
"borderWidth": 1,
"borderColor": "red",
"backgroundColor": "lightGray"
}
}
现在我们有了一个名为 "labelStyle" 的样式,它包含很多参数,我们已经开始使用它了,但是让我们声明第二个样式
{
"titleLabelStyle": {
"fontSize": 25,
"fontColor": "#7F007F",
"borderWidth": 1,
"borderColor": "red",
"backgroundColor": "lightGray"
}
}
好的,但是这两个样式使用相同的边框设置和相同的字体颜色,因此可以使用继承来改进声明
{
"defBorder": {
"borderWidth": 1,
"borderColor": "red",
"backgroundColor": "lightGray"
},
"labelStyle": {
"parent": "defBorder",
"fontSize": 15,
"fontColor": "#7F007F"
},
"titleLabelStyle": {
"parent": "labelStyle",
"fontSize": 25
}
}
有关完整参数列表和完整语法描述,请查看 SKStyleKit 参数指南。
基本用法
使用样式的最便捷(但并非唯一!)方式是与 SK 组件或它们的子类一起使用。SK 组件已具有 style
和 styleName
属性
标签示例
在 xib 或 storyboard 文件中选择任何标签将其设置为 SKLabel 类。
然后切换到属性检查器并设置样式名称属性
这就完成了!
注意:样式也可以组合,如 "firstStyleName+secondStyleName+<...>"
按钮示例
SKButton具有针对不同状态的独立样式。让我们声明它们
{
"button.normal": {
"parents": ["button.Text", "button.ViewStyle"],
"alpha": 1
},
"button.selected": {
"backgroundColor": "green"
},
"button.highlighted": {
"alpha": 0.5
},
"button.disabled": {
"backgroundColor": "gray"
}
}
注意:正常状态的样式应设置将要在其他状态样式中更改的所有参数
然后设置它们在接口图像中
高级
以编程方式处理样式
SKStyleKit的入口点是StyleKit类。
为了获取特定样式的实例
let style = StyleKit.style(withName: "StyleName")
在非SK组件中使用样式包
样式可以应用于任何标准控件,例如
let style = StyleKit.style(withName: "StyleName")
style.apply(view: UIView?)
style.apply(slider: UISlider?)
style.apply(progress: UIProgressView?)
style.apply(label: UILabel?, text: String?)
style.apply(button: UIButton?, title: String?, forState state: UIControlState)
style.apply(switchControl: UISwitch?)
style.apply(textField: UITextField?, text: String?)
style.apply(textView: UITextView?, text: String?)
但在这种情况下,您不应直接设置文本或attributedText属性,而应使用适当的SKStyle方法。
使用属性字符串与样式包
样式也可以应用于字符串/属性字符串
{
"header3": {
"fontSize": 25,
"fontColor": "#7F007F"
}
}
let s = "Some string"
let style = StyleKit.style(withName: "header3")
SKStyle.string(withStyle: style, string: s) -> NSAttributedString?
SKStyle.string(withStyle: style, attributedString: NSAttributedString(string: s)) -> NSAttributedString?
这些函数还允许指定应用样式的范围,例如
{
"header1": {
"fontSize": 25,
"fontColor": "#7F007F"
}
}
let s = "Some string"
let style = StyleKit.style(withName: "header1")
SKStyle.string(withStyle: style, string: s, range: NSRange(location: 0, length: 5)) -> NSAttributedString?
SKStyle.string(withStyle: style, attributedString: NSAttributedString(string: s), range: NSRange(location: 0, length: 8)) -> NSAttributedString?