Is there any way of using a gradient as foregroundColor of Text in SwiftUI?
Thanks for the answers in advance!
Step 1: Apply a basic background to the body tag and align the text to center of the page. Step 2: Do some basic styling like font-size and family etc. Step 3: Apply the linear gradient property with any colors of your choice.
What might surprise you — especially if you have prior experience with design tools — is that you can't set a gradient as the text color directly. For example color: linear-gradient(yellow, red) won't work. But gradient text can be achieved in CSS, it just requires a few extra steps.
Select the Gradient tool in the toolbar. In the selected artwork you'll see the gradient annotator, which shows the gradient slider and the color stops. Double-click a color stop on the artwork to edit the color, drag the color stops, click beneath the gradient slider to add new color stops, and more.
This can be easily done in pure SwiftUI
without using UIViewRepresentable
. You need to mask a gradient with your text:
LinearGradient(gradient: Gradient(colors: [.pink, .blue]),
startPoint: .top,
endPoint: .bottom)
.mask(Text("your text"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With