Interpolate Text with Custom ForegroundStyle in SwiftUI

KD Knowledge Diet
2 min readJun 5, 2024

--

In SwiftUI, text interpolation allows us to apply custom foreground styles to specific portions of a Text view. This feature is incredibly powerful for creating visually engaging and stylized text within your app’s user interface. With the introduction of iOS 17, SwiftUI has further enhanced text styling capabilities by allowing us to use foregroundStyle() to apply intricate styles, including gradients, to ranges of text within a Text view.

In this guide, we’ll explore how to interpolate text and apply custom foreground styles to create visually appealing text elements in SwiftUI.

Basic Text Interpolation:

Text interpolation involves embedding one Text view within another and applying text modifiers to customize the appearance of the embedded text. This approach provides a convenient way to style specific portions of the text differently.

Here’s a basic example of text interpolation in SwiftUI:

Text("Hello, \(Text("world").foregroundColor(.blue))!")
.bold()
.font(.title)
.textCase(.uppercase)

In this example:

1. We interpolate the word “world” within the main text by placing it inside another Text view.

2. We apply the `.foregroundColor(.blue)` modifier to the interpolated text, making the word “world” appear in blue.

3. Additional modifiers like `.bold()`, `.font()`, and `.textCase()` are used to further style the entire Text view.

Advanced Text Styling with foregroundStyle():

Starting from iOS 17, SwiftUI introduces the foregroundStyle() modifier, which allows us to apply more intricate foreground styles to text ranges. One powerful use case is applying gradients to specific portions of text. Let’s look at an example:

struct ContentView: View {
let gradient = LinearGradient(
colors: [.blue, .green],
startPoint: .leading,
endPoint: .trailing
)

var body: some View {
Text("Hello, \(Text("world").foregroundStyle(gradient))!")
.bold()
.font(.title)
.textCase(.uppercase)
}
}

In this code:

1. We define a `LinearGradient` called `gradient` with blue and green colors, creating a gradient effect.

2. We interpolate the word “world” within the main text and apply the `.foregroundStyle(gradient)` modifier to it. This results in the word “world” having a gradient foreground color.

3. The `Text` view is styled with additional modifiers like `.bold()`, `.font()`, and `.textCase()` to create a visually appealing text element.

Leveraging ShaderLibrary:

For even more advanced text styling, iOS 17 introduces ShaderLibrary, which allows you to create custom shaders that can be converted into ShapeStyles and then applied using foregroundStyle(). This opens up a world of possibilities for creating unique and dynamic text effects.

Conclusion:

With SwiftUI’s text interpolation and the foregroundStyle() modifier introduced in iOS 17, you have powerful tools at your disposal for creating customized and visually engaging text elements in your app. Whether you want to apply gradients, shaders, or other intricate styles to specific portions of your text, SwiftUI makes it easier than ever to achieve your desired text effects. Experiment with these features to bring your app’s user interface to life with beautifully styled text! 🎨📝

--

--

KD Knowledge Diet

Software Engineer, Mobile Developer living in Seoul. I hate people using difficult words. Why not using simple words? Keep It Simple Stupid!