Swiftpack.co - davdroman/TextBuilder as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by davdroman.
davdroman/TextBuilder 3.0.1
Like a SwiftUI ViewBuilder, but for Text
⭐️ 221
🕓 29 weeks ago
iOS macOS watchOS tvOS
.package(url: "https://github.com/davdroman/TextBuilder.git", from: "3.0.1")

TextBuilder

CI status

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 Result Builders to solve this problem. TextBuilder mimics SwiftUI's ViewBuilder to make for a familiar experience at the point of use.

Usage

TextBuilder offers 3 ready-made builders out of the box, depending on which text separator you need.

Default (unspaced)

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

With Spaces

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

Multiline

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

Pro Tip ✨

TextBuilder accepts String types directly as if they were plain Text, and also provides a String.text computed var to remove unwanted code noise when Text is explicitly needed.

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

Other Separators

There are two options to customize the separator used to compose your Text.

First, you can use Text.init(separator:content:):

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

But if you prefer to keep using a result builder, you can:

struct EggplantSeparator: TextBuilderSeparator {
    static var separator: String? { " 🍆 " }
}

@TextBuilderWith<EggplantSeparator>
var loremIpsum: Text {
    "Lorem".text.underline().foregroundColor(.blue)
    "ipsum dolor"
    "sit".text.bold()
    "amet, consectetur"
}

Benchmarks

MacBook Pro (14-inch, 2021)
Apple M1 Pro (10 cores, 8 performance and 2 efficiency)
32 GB Memory

$ swift run -c release Benchmarks

name           time        std        iterations
------------------------------------------------
Result Builder 1875.000 ns ±  26.15 %     729940
Initializer    2542.000 ns ±  16.88 %     540826

GitHub

link
Stars: 221
Last commit: 29 weeks ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

3.0.1
29 weeks ago

What's Changed

Full Changelog: https://github.com/davdroman/TextBuilder/compare/3.0.0...3.0.1

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