AttributedStringBuilder

AttributedStringBuilder 是一个简单的构建器,它通过在闭包中表达每个组件,使组合 NSAttributedString 变得愉快。

Tests

特性

它提供了两种组合属性字符串的方式。第一种方式是通过使用新的 Swift 5.3 函数构建器特性来实现的。

在闭包中组合

NSAttributedString {
    "Doesn't that".with(attributes: .font(.boldSystemFont(ofSize: 34)))
    Space()
    Text("look", with: .textColor(.blue))
    "wonderful?"
    NewLine()
}

使用链式调用组合

NSAttributedString.Builder()
    .add("Doesn't", with: .font(.boldSystemFont(ofSize: 34)))
    .add(.space)
    .add("that", with: .textColor(.blue))
    .add("either?")
    .add(.newLine)
    .build()

要求

安装

使用 Swift Package Manager

选择 File > Swift Packages > Add Package Dependency 并输入仓库 URL。

https://github.com/kkiermasz/AttributedStringBuilder

用法

导入 AttributedStringBuilder

import AttributedStringBuilder

并使用之前介绍的方式之一来组合你出色的属性字符串。

演示

playground 演示可以在与项目关联的工作区中找到。请记住,您必须首先构建库。

许可证

AttributedStringBuilder 在 MIT 许可证下共享。 有关更多信息,请参见 LICENSE