Skip to content

About

Like a SwiftUI ViewBuilder, but for Text

Resources

Stars

247 stars

Watchers

3 watching

Forks

Repository files navigation

TextBuilder

CI

Introduction

Text composition in SwiftUI can often be cumbersome, especially when there's logic affecting its format and content.

TextBuilder leverages the power of Swift Macros to solve this problem. The @TextBuilder macro transforms computed properties into builder-style closures, making text composition intuitive and readable.

Requirements

Swift 6.4 (Xcode 27) or later, targeting iOS 15, Mac Catalyst 15, macOS 12, tvOS 15, visionOS 1, or watchOS 8 or later.

Installation

Add TextBuilder to your Swift Package Manager dependencies:

.package(url: "https://github.com/davdroman/swiftui-text-builder", from: "5.0.0"),

Then, add the dependency to your desired target:

.product(name: "TextBuilder", package: "swiftui-text-builder"),

Usage

Basic Usage

Apply @TextBuilder to computed properties that return Text. The macro will transform the property body into a builder-style closure that concatenates text segments.

@TextBuilder
var loremIpsum: Text {
    Text("Lorem").underline().foregroundColor(.blue)
    Text("ipsum dolor")
    Text("sit").bold()
    Text("amet, consectetur")
}

This creates a concatenated Text without any separators between segments.

@TextBuilder can also be applied to a property's getter or to a function:

var loremIpsum: Text {
    @TextBuilder get {
        Text("Lorem").underline().foregroundColor(.blue)
        Text("ipsum dolor")
    }
}

@TextBuilder
func loremIpsum() -> Text {
    Text("Lorem").underline().foregroundColor(.blue)
    Text("ipsum dolor")
}

With Separators

You can specify a separator to be inserted between text segments:

@TextBuilder(separator: " ")
var spacedText: Text {
    Text("Lorem").underline().foregroundColor(.blue)
    Text("ipsum dolor")
    Text("sit").bold()
    Text("amet, consectetur")
}

For multiline text:

@TextBuilder(separator: "\n")
var multilineText: Text {
    Text("Lorem").underline().foregroundColor(.blue)
    Text("ipsum dolor")
    Text("sit").bold()
    Text("amet, consectetur")
}

String Support ✨

TextBuilder accepts strings directly. String literals are localized, just like Text("…"), and string values are verbatim, just like Text(someString):

@TextBuilder(separator: " ")
var mixedText: Text {
    "Hello"  // String literal becomes localized Text
    "\(count) items"  // So does an interpolated literal
    Text("world").bold()  // Use Text for chaining modifiers
    String(2025)  // String values become verbatim Text
}

Because literals are localized, they behave like Text("…") in two more ways: Markdown in them is rendered, and Text values can be interpolated into them, keeping their styling:

@TextBuilder
var summary: Text {
    "**Bold** and _italic_"
    "\(Text("3").bold()) items left"
}

To show a literal exactly as written, use Text(verbatim:).

Attributed Strings and Images

AttributedString, LocalizedStringResource, and Image values can go in directly too:

@TextBuilder(separator: " ")
var status: Text {
    Image(systemName: "checkmark.circle")
    attributedTitle
    LocalizedStringResource("status.done")
}

Localization

String literals are looked up in the bundle of the code they're written in, as #bundle gives: the app's bundle in an app, or the resource bundle of a framework or Swift package target. Their keys are extracted into that target's String Catalog.

To look them up somewhere else, pass bundle::

@TextBuilder(separator: " ", bundle: .main)
var greeting: Text {
    "Hello"
    "world"
}

Important

A Swift package target without resources has no bundle of its own, so #bundle fails to compile there with "No resource bundle is available for this module". Pass the bundle that holds its strings instead, or bundle: nil for the main bundle.

Control Flow

TextBuilder supports Swift's control flow statements:

@TextBuilder(separator: " ")
var conditionalText: Text {
    "Hello"

    if showDetails {
        "with details"
    } else {
        "basic"
    }

    if let name = userName {
        Text(name).italic()
    }

    for i in 1...3 {
        String(i)
    }
}

Alternative API

If you prefer not to use macros, you can use the underlying Text initializer directly:

var body: some View {
    Text(separator: " 👏 ") {
        Text("Lorem").underline().foregroundColor(.blue)
        "ipsum dolor"
        Text("sit").bold()
        "amet, consectetur"
    }
}

This is useful if you simply want to insert some rich text into a view body without defining a separate property.

Development

Install Mise, then install the project tools and Git hooks:

mise install
mise exec -- hk install --mise

About

Like a SwiftUI ViewBuilder, but for Text

Resources

Stars

247 stars

Watchers

3 watching

Forks

Releases

Used by

Contributors

Languages