UIStyle 1.2

UIStyle 1.2

测试已测试
语言语言 SwiftSwift
许可证 MIT
发布最新发布2016年11月
SwiftSwift 版本3.0
SPM支持 SPM

Igor Matyushkin 维护。




StyleKit

概述

前端开发者,在创建 HTML 页面的布局时,都知道 CSS 类的简洁和强大。我一度想:为什么不为原生 iOS 应用做类似的事情呢?这个想法非常明显。当第一个版本框架完成时,它被命名为 StyleKit。显而易见的名字。

入门

  • Source 文件夹的内容复制到您的项目中。

或者

  • 使用 UIStyle Cocoapod

要求

  • iOS 9.0 及以上
  • Xcode 8 及以上

使用方法

样式是一组 UI 属性。每个样式至少包含一个属性,但可以包含无限多的属性集合。

/*
 * Create simple style with one attribute.
 */
let attributes: [ViewStyleAttribute] = [
    .backgroundColor(color: .yellow)
]

let yellowBackground = ViewStyle(attributes: attributes)

/*
 * Another way to create the same style.
 */

let anotherYellowBackground = ViewStyle.with(attribute: .backgroundColor(color: .yellow))
    .done()

/*
 * Create style with multiple attributes.
 */

let greenBackgroundWithThinRedBorder = ViewStyle.with(attribute: .backgroundColor(color: .green))
    .and(attribute: .borderColor(color: .red))
    .and(attribute: .borderWidth(width: 1.0))
    .done()

任何样式都可以应用到任何视图。您可以将无限数量的样式应用到同一视图。

/*
 * Apply style to view.
 */

view.stl.apply(style: yellowBackground)

/*
 * Apply multiple styles to view.
 */

view.stl.apply(style: yellowBackground)
    .apply(style: greenBackgroundWithThinRedBorder)

管理应用程序中的样式推荐方式是实现具有静态样式的结构

struct StyleStorage {

    static let defaultBackground = ViewStyle.with(attribute: .backgroundColor(color: .white))
        .and(attribute: .borderColor(color: .green))
        .and(attribute: .borderWidth(width: 2.0))
        .done()

    static let thinOrangeText = ViewStyle.with(attribute: .textColor(color: .orange))
        .and(attribute: .font(font: UIFont.systemFont(ofSize: 36.0, weight: UIFontWeightThin)))
        .done()

}

您可以在应用程序的不同地方多次重用这些样式

override func viewDidLoad() {
    super.viewDidLoad()

    /*
     * Initialize view.
     */

    view.stl.apply(style: StyleStorage.defaultBackground)

    /*
     * Initialize title label.
     */

    titleLabel.stl.apply(style: StyleStorage.thinOrangeText)
}

您还可以以程序方式检查样式是否支持视图

if StyleStorage.thinOrangeText.supports(view: helloLabel) {
    helloLabel.stl.apply(style: StyleStorage.thinOrangeText)
}

许可证

StyleKit 受 MIT 许可证保护。有关更多信息,请参阅 LICENSE 文件。