BlowMindStyle 0.23.1

BlowMindStyle 0.23.1

Aleksey Gotyanov 维护。



 
依赖项
RxSwift> 5.0
RxCocoa> 5.0
SemanticString> 0.23
 

  • Gotyanov

Cocoapods platforms pod Swift Package Manager compatible

简介

BlowMindStyle 库的目的是提供应用程序样式的基础设施。 BlowMindStyle 允许:

  • 编写可重用的视图样式
  • 编写可重用的文本格式化样式(基于 SemanticString
  • 添加应用程序主题
  • 根据 traits 集合和主题动态更新视图
  • 根据模型状态动态更新视图。

基本用法

  1. 添加用于样式的资源
struct ButtonProperties {
    var backgroundColor: UIColor?
    var cornerRadius: CGFloat?
    var titleColor: UIColor?
    var font: UIFont?
    var contentEdgeInsets: UIEdgeInsets?
}
  1. 定义样式
final class ButtonStyle<Environment: StyleEnvironmentType>: EnvironmentStyle<ButtonProperties, Environment> { }
  1. 将资源应用于视图
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
        }
    }
}
  1. 使用样式
button.setUpStyles {
    $0.buttonStyle.apply(.primary)
}

更多信息请见 教程

依赖项

安装

CocoaPods

# Podfile
use_frameworks!

target 'YOUR_TARGET_NAME' do
   pod 'BlowMindStyle'
end

SwiftPackageManager

在XCode中选择文件/Package依赖项/添加Package依赖项。输入'BlowMindStyle',选择BlowMindStyle项目,然后点击'下一步','下一步'

教程

  1. 项目准备
  2. 创建自己的风格
  3. 创建主题
  4. 根据状态切换样式
  5. 文本样式化