Popularity
2.9
Growing
Activity
6.8
-
155
2
4

Programming language: Swift
License: The Unlicense
Tags: Text    

TextBuilder alternatives and similar libraries

Based on the "Text" category.
Alternatively, view TextBuilder alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of TextBuilder or a related project?

Add another 'Text' Library

README

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.

Unspaced Text

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

[](Tests/TextBuilderTests/Snapshots/TextBuilderTests/testBasicTextBuilder.iOS.png)

Spaced Text

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

[](Tests/TextBuilderTests/Snapshots/TextBuilderTests/testSpacedTextBuilder.iOS.png)

Multiline Text

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

[](Tests/TextBuilderTests/Snapshots/TextBuilderTests/testMultilineTextBuilder.iOS.png)

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.

@MultilineTextBuilder
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 { " 🍆 " }
}

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

[](Tests/TextBuilderTests/Snapshots/TextBuilderTests/testCustomTextBuilder.iOS.png)

Try it out!

TextBuilder supports Arena to effortlessly take it for a spin in a playground before you decide to add it to your codebase.

Simply install Arena and run arena davdroman/[email protected]:master --platform macos in your terminal.