AttributedStringBuilder is a simple builder that makes composing NSAttributedString pleasant by expressing each component within a closure.
It provides two ways of composing attributed strings. The first one is achieved by using new Swift 5.3 function builder feature.
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 5.3+
- Xcode 12.0+
- iOS 11.0+ / macOS 10.11+ / tvOS 11.0+
Using Swift Package Manager
Select File > Swift Packages > Add Package Dependency and enter the repository URL.
https://github.com/kkiermasz/AttributedStringBuilder
Import the AttributedStringBuilder
import AttributedStringBuilder
And compose your awesome attributed string in one of previously introduced ways.
The playground demo can be found in the workspace associated with the project. remember that you have to build the library first.
AttributedStringBuilder is shared under the MIT license. See the LICENSE for more info.