Freestyler 1.0.0

Freestyler 1.0.0

测试测试过
语言语言 SwiftSwift
许可证 MIT
发布最后发布2016年12月
SwiftSwift 版本3.0
SPM支持 SPM

Alexander DolozAlexander Doloz 维护。



  • Alexander Doloz

Freestyler

是什么?

Freestyler 允许你使用声明性语法在单个位置定义应用的样式,这样你可以轻松地在整个应用中更改颜色、字体和其他视觉属性。

如何使用它?

  • 设置标签文字颜色为红色:swift label <~ UILabel.style(textColor: UIColor.red)
  • 将样式保存到某个变量:swift let redTextColor = UILabel.style(textColor: UIColor.red)
  • 将上述样式应用到多个标签:swift [label1, label2] <~ redTextColor
  • 使文字颜色为绿色且字体 加粗 的样式:swift let greenTextColorAndBoldFont <~ UILabel.style(textColor: UIColor.green) <~ UILabel.style(font: UIFont.boldSystemFont(ofSize: 14.0)) /// 或 let greenTextColorAndBoldFont <~ [UILabel.style(textColor: UIColor.green), UILabel.style(font: UIFont.boldSystemFont(ofSize: 14.0))] /// 或 let greenTextColorAndBoldFont: Style = [UILabel.style(textColor: UIColor.green), UILabel.style(font: UIFont.boldSystemFont(ofSize: 14.0))]
  • 样式的应用不是可交换的,即顺序很重要:swift label <~ [redTextColor, greenTextColorAndBoldFont] // ^ 标签将是绿色且加粗 label <~ [greenTextColorAndBoldFont, redTextColor] // ^ 标签将是红色;尽管如此,它仍然是加粗的
  • 你可以将 UIView 的样式应用到 UILabel,但不能反过来;通常,你可以将类的样式应用到子类的实例:swift label <~ UIView.style(backgroundColor: UIColor.yellow) // 下面的代码将崩溃 🔥 view <~ UILabel.style(textColor: UIColor.red)
  • 使用协议 Color,你可以为应用中经常遇到的颜色创建调色板并给予有意义的名字。它还将帮助你有一个所有颜色都集中存放的地方,这样你可以用一行代码改变整个应用中的颜色。

    enum Theme: Color {
        case title
        case subtitle
        case background
    
        var color: UIColor {
            switch self {
            case .title: return UIColor.black
            case .subtitle: return UIColor.lightGray
            case .background: return UIColor.white
            }
        }
    }
    
    // ... somewhere in code
    
    label <~ UILabel.style(textColor: Theme.title)
  • 同样,字体也有类似的 Font 协议。

    enum Typography: Font {
        case big
        case medium
        case small
    
        var font: Font {
            switch self {
            case .big: return UIFont.systemFont(ofSize: 18.0)
            case .medium: return UIFont.systemFont(ofSize: 14.0)
            case .small: return UIFont.systemFont(ofSize: 11.0)
            }
        }
    }
    
    // ... somewhere in code
    
    label <~ UILabel.style(font: Typography.big)

要求

  • iOS 8.0+
  • Swift 3
  • Xcode 8.0+

安装

手动安装

  1. 克隆或下载 Freestyler
  2. Freestyler 文件夹拖到 Xcode 的项目中;请确保 ☑️ 将项目中的内容复制到目标组的文件夹中(如果需要) 选项已勾选。

作者

亚历山大·多洛兹,[email protected]

许可

Freestyler 在 MIT 许可下可用。更多详细信息请参阅 LICENSE.txt 文件。