UIKitSwiftDSL 1.0

UIKitSwiftDSL 1.0

Majid Jabrayilov 维护。



UIKitSwiftDSL

Version Carthage compatible License Platform

Swift DSL for UIKit。一个简单的 Swift DSL,让您能够以可读和声明的方式编写布局代码。

用法

let rootView = stack {
    $0.spacing = 16
    $0.axis = .vertical
    $0.isLayoutMarginsRelativeArrangement = true

    $0.stack {
        $0.distribution = .fillEqually
        $0.axis = .horizontal

        $0.label {
            $0.textAlignment = .center
            $0.textColor = .white
            $0.text = "Hello"
        }

        $0.label {
            $0.textAlignment = .center
            $0.textColor = .white
            $0.text = "World"
        }

        $0.customLabel {
            $0.textAlignment = .center
            $0.textColor = .white
            $0.text = "!!!"
        }
    }

    let messageButton = $0.button {
        $0.tintColor = .white
        $0.setTitle("Say Hi!", for: .normal)
    }

    $0.view {
        $0.backgroundColor = .clear
    }
}

自定义视图

如果您在项目中使用了一些自定义视图,可以很容易地添加它以支持 DSL。

import UIKitSwiftDSL

class CustomLabel: UILabel {
// Custom implementation here
}

extension UIView {
    @discardableResult
    func customLabel(apply closure: (CustomLabel) -> Void) -> CustomLabel {
        return custom(CustomLabel(), apply: closure)
    }
}

自动布局

UIKitSwiftDSL 没有任何与 AutoLayout 相关的逻辑。您可以使用任何希望使用的库。

$0.button {
    $0.tintColor = .white
    $0.setTitle("Say Hi!", for: .normal)
    
    // Add your AutoLayout code here.
}

示例

要运行示例项目,请克隆仓库,然后首先从示例目录运行pod install

安装

UIKitSwiftDSL可通过CocoaPods获取。要安装它,只需将以下行添加到您的Podfile中

pod 'UIKitSwiftDSL'

UIKitSwiftDSL可通过Carthage获取。要安装它,只需将以下行添加到您的Cartfile中

github "mecid/UIKitSwiftDSL"

作者

Majid Jabrayilov,[email protected]

许可

UIKitSwiftDSL受MIT许可。有关更多信息,请参阅LICENSE文件。