Simple extension to set gradient color to Text() in SwiftUI

Sunil Targe
Apr 29, 2021

--

Simple Swift Extention that will help to set the gradient foreground colour to your Text(“Hello world!”) in SwiftUI.

extension View {
public func gradientForeground(colors: [Color]) -> some View {
self.overlay(LinearGradient(gradient: .init(colors: colors),
startPoint: .topLeading,
endPoint: .bottomTrailing))
.mask(self)
}
}

NOTE: You can change startPoint and endPoint position as per your requirements.

How to use it?

Text("Hello world!")
.gradientForeground(colors: [.red, .blue])
.padding()
Result as a gradient Text 🤩

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Sunil Targe
Sunil Targe

No responses yet

Write a response