Swiftpack.co -  ethanhuang13/NSAttributedStringBuilder as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Composing NSAttributedString with SwiftUI-style syntax
.package(url: "https://github.com/ethanhuang13/NSAttributedStringBuilder.git", from: "0.4.1")


Build Status codecov GitHub release GitHub top language License Twitter Donate

Composing NSAttributedString with SwiftUI-style syntax, powered by Result Builder.

Project Link: https://github.com/ethanhuang13/NSAttributedStringBuilder


🐦 Open source library written in Swift 5.4
🍬 SwiftUI-like syntax
💪 Support most attributes in NSAttributedString.Key
📦 Distribution with Swift Package Manager
🧪 Fully tested code
🛠 Continuously integrates in Swift Source Compatibility Suite

How to use?

Traditionally we compose a NSAttributedString like this:

let mas = NSMutableAttributedString(string: "")
mas.append(NSAttributedString(string: "Hello world", attributes: [.font: UIFont.systemFont(ofSize: 24), .foregroundColor: UIColor.red]))
mas.append(NSAttributedString(string: "\n"))
mas.append(NSAttributedString(string: "with Swift", attributes: [.font: UIFont.systemFont(ofSize: 20), .foregroundColor: UIColor.orange]))

Now, with NSAttributedStringBuilder, we can use SwiftUI-like syntax to declare NSAttributedString:

let attributedString = NSAttributedString {
    AText("Hello world")
        .font(.systemFont(ofSize: 24))
    AText("with Swift")
        .font(.systemFont(ofSize: 20))


Xcode 12.5. This project uses Swift 5.4 feature Result Builder.


Swift Package

Open your project in Xcode 12, navigate to Menu -> Swift Packages -> Add Package Dependency and enter https://github.com/ethanhuang13/NSAttributedStringBuilder to install.


Add pod 'NSAttributedStringBuilder13' to your Podfile.

SwiftUI Sample Project

Besides clearer NSAttributedString syntax, since NSAttributedStringBuilder uses Result Builder it also enables API to build components in UIViewRepresentable(which embeds UIView in a SwiftUI View).

Just like a SwiftUI Text takes a String as input, the purpose of AttributedText in the sample project is to take a NSAttributedString as input and render in SwiftUI.

To achieve this, AttributedText.swift uses @NSAttributedStringBuilder to support SwiftUI-style syntax:


Then using an AttributedText will be like: ContentView.swift

Open the sample in /SwiftUISampleApp/AttributedTextSample.xcodeproj and check AttributedText. It uses UITextView, you can also use UILabel or NSTextView.


  • Better tests for image attachment

Known Issue

  • NSAttributedString does not support link color, therefore Link component with a .color() modifier has no effect. Alternatively you need to specify in UITextView.linkTextAttributes or .tintColor.


Initially discussed on this Twitter thread. Some code are inspired by zonble🙏.


Stars: 683
Last commit: 1 week ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

1 year ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API