UIKitSimplify 0.1.0

UIKitSimplify 0.1.0

Anjas Dwi 维护。



iOS Swift

UIKitSimplify

UIKitSimplify 通过采用类似 SwiftUI 的领域特定语言 (DSL) 方法简化了 UIKit。

内容

要求

  • iOS 12.0+
  • Xcode 10.0+
  • Swift 5.0+

安装

UIKitSimplify 支持使用 Cocoapod 进行安装,您可以通过将库添加到 PodFile 来使用此库。

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '12.0'
use_frameworks!

target '<Your Target Name>' do
    pod 'UIKitSimplify', '~> 0.1.0'
end

使用

创建对象

使用 Withable 自定义协议来帮助您轻松创建对象并配置它们的属性。只需使用 with 函数,您就可以访问所有对象属性。例如。

尾随语法

使用 尾随闭包语法 的示例

let mainvView = UIView().with { view
    view.backgroundColor = .gray
    view.layer.cornerRadius = 4
}
简写语法

使用 简写闭包语法 的示例

let mainView = UIView().with {
    $0.backgroundColor = .gray
    $0.layer.cornerRadius = 4
}

使用 ResultBuilder 来排列视图

具有名称 ViewBuilder 的 ResultBuilder,用于 DSL 方法来创建和排列视图,类似于 SwiftUI 中的 UIStackViewUIView。您可以初始化视图并直接排列要添加的子视图。例如。

使用初始化方法

使用初始化方法的示例

let mainSV = UIStackView {
        UIlabel()
        UILabel().with {
            $0.backgroundColor = .gray
            $0.layer.cornerRadius = 4
        }
    }
使用现有函数

所有继承自 UIStackView 的对象都可以使用 ViewBuilder 方法与 addArrangedSubviews 一起使用。例如。

let mainSV = UIStackView()
    .addArrangedSubviews {
        UIlabel()
    }

同时,UIView 可以使用 ViewBuilder 方法与 addSubViews 一起使用。例如。

let mainSV = UIView()
    .addSubViews {
        UIlabel()
    }

UIStackView DSL

使用 Withable 自定义协议为 UIStackview DSL 添加了一些功能。

let mainSV = UIStackView()
    .axis(.vertical)
    .distribution(.fill)
    .spacing(4)
    .views(
        anotherView1,
        anotherView2,
        anotherView3
    )
    .setCustomSpacing(
        (8, after: anotherView1),
        (12, after: anotherView2)
    )

结合 Withable 和 ViewBuilder 方法

let mainSV = UIStackView {
        anotherView1
        anotherView2
        anotherView3
    }
    .axis(.vertical)
    .distribution(.fill)
    .spacing(4)
    .setCustomSpacing(
        (8, after: anotherView1),
        (12, after: anotherView2)
    )

许可证

遵守 MIT 许可证。有关更多信息,请参阅 LICENSE.txt

联系

Anjas Dwi - LinkedIn - [email protected]
项目链接: https://github.com/anjasdwi/UIKitSimplify
Cocoapod 链接: https://cocoapods.org.cn/pods/UIKitSimplify