Skip to content
Composing NSAttributedString with SwiftUI-style syntax
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
.swiftpm/xcode [Added] ImageAttachment Jul 11, 2019
Sources/NSAttributedStringBuilder [Removed] OS version requirements of Package.swift #1 Jul 19, 2019
SwiftUISampleApp [Changed] Replace UIKitForMac to macCatalyst to reflect the changes i… Jul 18, 2019
Tests [Removed] OS version requirements of Package.swift #1 Jul 19, 2019
.gitignore [Added] NSAttributedStringBuilder package Jul 9, 2019
.travis.yml [Added] codecov Jul 12, 2019 [Removed] OS version requirements of Package.swift #1 Jul 19, 2019
Package.swift [Removed] OS version requirements of Package.swift #1 Jul 19, 2019 Update Aug 1, 2019
demo.jpg [Changed] Update screenshots in README Jul 18, 2019
demo2.jpg [Changed] Update screenshots in README Jul 18, 2019


Build Status codecov GitHub release GitHub top language License Twitter Donate

Composing NSAttributedString with SwiftUI-style syntax, powerd by the now pitching Function Builder.

Project Link:


🐦 Open source library written in Swift 5
🍬 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:]))
mas.append(NSAttributedString(string: "\n"))
mas.append(NSAttributedString(string: "with Swift", attributes: [.font: UIFont.systemFont(ofSize: 20), .foregroundColor:]))

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 11 beta 4. This project uses Swift 5.1 feature Function Builder.


Please use Swift Package Manager to install.

Open your project in Xcode 11, navigate to Menu -> Swift Packages -> Add Package Dependency and enter to install.

SwiftUI Sample Project

Besides clearer NSAttributedString syntax, since NSAttributedStringBuilder uses Function Builder it also enables API to build components in UIViewRepresentable(which embbed 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🙏.

You can’t perform that action at this time.