Sitemap
DevTechie

DevTechie.com is all about sharing knowledge by practical means. We focus on solving real world challenges via code.

New in SwiftUI 3: foregroundStyle

3 min readOct 15, 2021

--

Introduced in iOS 15 and SwiftUI 3, foregroundStyle is a new modifier which can be used to apply color or pattern as foreground to view. This modifier can be applied to style content of text, shapes, symbols, images etc.

Let’s start by setting foreground style with orange color:

struct ForegroundStyleExample: View {
var body: some View {
VStack {
Text("DevTechie")
Image(systemName: "globe")
Capsule()
.frame(height: 50)
.overlay {
Text("SwiftUI 3")
.foregroundColor(.pink)
}
}
.font(.system(size: 42))
.foregroundStyle(Color.orange)
}
}
Press enter or click to view image in full size

Another use case of this can be to apply gradient on Text view.

Note: Prior iOS 15, adding gradient effect on Text would require custom implementation. If you would like to learn about that, check out this video on YouTube(while you are there hit the like and subscribe for me 😜): https://www.youtube.com/watch?v=m_EWD6LkVls

So back to applying gradient on Text, here is how it goes:

struct ForegroundStyleExample: View {
var body: some View {
VStack {
Text("DevTechie")
Image(systemName: "globe")
Capsule()…

--

--

DevTechie
DevTechie

Published in DevTechie

DevTechie.com is all about sharing knowledge by practical means. We focus on solving real world challenges via code.

No responses yet