Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradient as foreground color of Text in SwiftUI

Is there any way of using a gradient as foregroundColor of Text in SwiftUI?

Thanks for the answers in advance!

like image 571
wictorious Avatar asked Nov 22 '19 09:11

wictorious


People also ask

How do you apply gradient color to text?

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.

Can I use linear gradient for text color?

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.

How do you give a gradient a color?

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.


1 Answers

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"))

enter image description here

like image 168
LuLuGaGa Avatar answered Nov 03 '22 09:11

LuLuGaGa