TextReplacements

0.2.0

TextReplacements is a SwiftUI library that extends the Text view with ways to customize the rendering of any parts of its text.
danielsaidi/TextReplacements

What's New

2025-04-08T20:22:55Z

Ok, never before has an assumption so quickly become incorrect as in this case.

A few hours after the 0.1, @kevinrpb suggested a change that will not only make this library a million times (ish) better, but it also calls for a project name change.

The change is that this version replaces the TextReplacementView with Text initializers. This is SO much better, since it places the logic where it's expected. I first considered keeping the view as well, but I think it will just be confusing.

As a result of this change, this project will be renamed from TextReplacementView to TextReplacements, which aligns much better with the new design.

Thank you Kevin for suggesting this change!

Project Icon

Version Swift 6.1 Swift UI Documentation MIT License

TextReplacements

TextReplacements is a SwiftUI library that extends the Text view with ways to customize any parts of its text.

TextReplacements works on all major Apple platforms and is designed to be easy to use.

Installation

TextReplacements can be installed with the Swift Package Manager:

https://github.com/danielsaidi/TextReplacements.git

Usage

TextReplacements let you initialize a Text view with a string and one or multiple text replacements.

The text view in the preview above is created like this:

Text(
    "TextReplacements is a SwiftUI library that extends the Text view with ways to customize any parts of its text. The result is a Text with customized segments that can flow nicely over multiple lines.",
    replacements: [
        "TextReplacements": {
            Text($0)
                .font(.title)
                .fontWeight(.black)
                .fontDesign(.rounded)
                .foregroundColor(.green)
        },
        "SwiftUI": {
            Text($0)
                .font(.headline)
                .fontWeight(.black)
                .fontDesign(.rounded)
                .foregroundColor(.blue)
        },
        "Text": {
            Text($0)
                .fontWeight(.black)
                .fontDesign(.rounded)
                .foregroundColor(.black.opacity(0.6))
        },
        "customize": {
            Text($0)
                .italic()
                .underline()
                .font(.body)
                .fontWeight(.heavy)
                .fontDesign(.monospaced)
                .foregroundColor(.purple)
        },
        "par": {
            Text($0)
                .font(.headline)
                .fontWeight(.black)
                .fontDesign(.rounded)
                .foregroundColor(.red)
        },
        "can flow nicely over multiple lines": {
            Text($0)
                .foregroundColor(.orange)
        }
    ]
)

You can perform any modification that result in new Text views. This brings some limitations, like not being able to apply a background color, but lets you highlight certain parts of a text with very little effort.

Documentation

This package will be documented once it gets more than 100 stars.

Demo Application

This package will get a demo application once it gets more than 100 stars.

Support my work

You can sponsor me on GitHub Sponsors or reach out for paid support, to help support my open-source projects.

Your support makes it possible for me to put more work into these projects and make them the best they can be.

Contact

Feel free to reach out if you have questions or want to contribute in any way:

License

TextReplacements is available under the MIT license. See the LICENSE file for more info.

Description

  • Swift Tools 6.1.0
View More Packages from this Author

Dependencies

  • None
Last updated: Tue May 13 2025 01:15:08 GMT-0900 (Hawaii-Aleutian Daylight Time)