简介
BlowMindStyle 库的目的是提供应用程序样式的基础设施。 BlowMindStyle 允许:
- 编写可重用的视图样式
- 编写可重用的文本格式化样式(基于 SemanticString)
- 添加应用程序主题
- 根据 traits 集合和主题动态更新视图
- 根据模型状态动态更新视图。
基本用法
- 添加用于样式的资源
struct ButtonProperties {
var backgroundColor: UIColor?
var cornerRadius: CGFloat?
var titleColor: UIColor?
var font: UIFont?
var contentEdgeInsets: UIEdgeInsets?
}
- 定义样式
final class ButtonStyle<Environment: StyleEnvironmentType>: EnvironmentStyle<ButtonProperties, Environment> { }
- 将资源应用于视图
extension EnvironmentContext where Element: UIButton {
var buttonStyle: StylableElement<ButtonStyle<StyleEnvironment>> {
stylableElement { button, style, resources in
button.setTitleColor(resources.titleColor, for: .normal)
let cornerRadius = resources.cornerRadius ?? 0
if let normalColor = resources.backgroundColor {
let normalBackground = UIImage.resizableImage(withSolidColor: normalColor, cornerRadius: cornerRadius)
button.setBackgroundImage(normalBackground, for: .normal)
} else {
button.setBackgroundImage(nil, for: .normal)
}
button.titleLabel?.font = resources.font ?? UIFont.systemFont(ofSize: UIFont.buttonFontSize)
button.contentEdgeInsets = resources.contentEdgeInsets ?? .zero
}
}
}
- 使用样式
button.setUpStyles {
$0.buttonStyle.apply(.primary)
}
更多信息请见 教程
依赖项
安装
CocoaPods
# Podfile
use_frameworks!
target 'YOUR_TARGET_NAME' do
pod 'BlowMindStyle'
end
SwiftPackageManager
在XCode中选择文件/Package依赖项/添加Package依赖项。输入'BlowMindStyle',选择BlowMindStyle
项目,然后点击'下一步','下一步'