Doná

Doná (在塔吉克语中意为种子) 是一个基于 Apple 的 UIKit 的库,它帮助你创建你自己的声明式 UI。

使用示例

更多信息请参考 Wiki。

UImageView 示例

private let imageView = UIImageView()
    .decorated(with: .namedImage("ExampleLogo"))
    .decorated(with: .contentMode(.scaleAspectFit))
    .decorated(with: .disabledAutoresizingMask())

UILabel 示例

private let titleLabel = UILabel()
    .decorated(with: .text("Hello, World!"))
    .decorated(with: .textColor(.systemOrange))
    .decorated(with: .textAlignment(.center))
    .decorated(with: .font(.systemFont(ofSize: 16, weight: .medium)))
    .decorated(with: .disabledAutoresizingMask())

如何创建你自己的自定义 ViewDecorator?

你可以使用 extension 扩展 ViewDecorator 结构体来扩展装饰器,以实现你自己的设计系统 (了解什么是设计系统)。例如

import DonaCore

extension DonaViewDecorator {
    static var largeTitle: DonaViewDecorator<UILabel> {
        DonaViewDecorator<UILabel> {
            $0.decorated(with: .disabledAutoresizingMask())
                .decorated(with: .font(.systemFont(ofSize: 29, weight: .bold)))
                .decorated(with: .textColor(.primary))
                .decorated(with: .numberOfLines(0))
        }
    }
}

在组合或创建新的装饰器之后,你可以像往常一样使用它。

private let titleLabel = UILabel()
    .decorated(with: .text("Welcome 👋"))
    .decorated(with: .largeTitle)