Simple extension to set gradient color to Text() in SwiftUI
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()
