AttributedStringBuilder 是一个简单的构建器,它通过在闭包中表达每个组件,使组合 NSAttributedString 变得愉快。
它提供了两种组合属性字符串的方式。第一种方式是通过使用新的 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()
选择 File > Swift Packages > Add Package Dependency 并输入仓库 URL。
https://github.com/kkiermasz/AttributedStringBuilder
导入 AttributedStringBuilder
import AttributedStringBuilder
并使用之前介绍的方式之一来组合你出色的属性字符串。
playground 演示可以在与项目关联的工作区中找到。请记住,您必须首先构建库。
AttributedStringBuilder 在 MIT 许可证下共享。 有关更多信息,请参见 LICENSE。