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.
TextReplacements can be installed with the Swift Package Manager:
https://github.com/danielsaidi/TextReplacements.git
TextReplacements let you create a Text with one or multiple text replacements. For instance, the preview above was created like this:
Text(
"TextReplacements is a SwiftUI library that extends the Text view...",
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(.primary.opacity(0.6))
}
...
]
)Since each text replacement must return a Text result, we can only apply view modifiers that return a Text. This brings some limitations, like not being able to apply a background color to individual components.
TextReplacements lets you initialize a Text view with a collection of texts and links:
Text(
.text("You must accept our"),
.link("terms & conditions", .init(string: "https://danielsaidi.com")),
.text("to use this app. See our"),
.link("website", .init(string: "https://danielsaidi.com")),
.text("for more information.")
)
.textLinkStyle(.boldItalic)
.accentColor(.orange)
.foregroundColor(.blue)
.padding()The resulting text is rendered as Markdown, which makes it possible to use plain view modifiers to style the view.
The online documentation has more information, articles, code examples, etc.
This package will get a demo application once it gets more than 100 stars.
You can become a sponsor to help me dedicate more time on my various open-source tools. Every contribution, no matter the size, makes a real difference in keeping these tools free and actively developed.
Feel free to reach out if you have questions or want to contribute in any way:
- Website: danielsaidi.com
- E-mail: daniel.saidi@gmail.com
- Bluesky: @danielsaidi@bsky.social
- Mastodon: @danielsaidi@mastodon.social
TextReplacements is available under the MIT license. See the LICENSE file for more info.
