danielsaidi/TextReplacements
Customize the look of any part of a text in a SwiftUI Text view.
Installation
TextReplacements can be installed with the Swift Package Manager:
https://github.com/danielsaidi/TextReplacements.gitGetting Started
Text Replacements
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.
Text 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.
Documentation
The online [documentation][Documentation] has more information, articles, code examples, etc.
Demo Application
This package will get a demo application once it gets more than 100 stars.
Support My Work
You can [become a sponsor][Sponsors] to help me dedicate more time on my various [open-source tools][OpenSource]. Every contribution, no matter the size, makes a real difference in keeping these tools free and actively developed.
Contact
Feel free to reach out if you have questions or want to contribute in any way:
- Website: [danielsaidi.com][Website]
- E-mail: [daniel.saidi@gmail.com][Email]
- Bluesky: [@danielsaidi@bsky.social][Bluesky]
- Mastodon: [@danielsaidi@mastodon.social][Mastodon]
License
TextReplacements is available under the MIT license. See the [LICENSE][License] file for more info.
[Email]: mailto:daniel.saidi@gmail.com [Website]: https://danielsaidi.com [GitHub]: https://github.com/danielsaidi [OpenSource]: https://danielsaidi.com/opensource [Sponsors]: https://github.com/sponsors/danielsaidi
[Bluesky]: https://bsky.app/profile/danielsaidi.bsky.social [Mastodon]: https://mastodon.social/@danielsaidi [Twitter]: https://twitter.com/danielsaidi
[Documentation]: https://danielsaidi.github.io/TextReplacements/ [License]: https://github.com/danielsaidi/TextReplacements/blob/master/LICENSE
Package Metadata
Repository: danielsaidi/TextReplacements
Homepage: https://danielsaidi.com/opensource
Stars: 102
Forks: 2
Open issues: 0
Default branch: main
Primary language: shell
License: MIT
Topics: ios, macos, swiftui, text, tvos, visionos, watchos
README: README.md