I used Gradient with a .mask(_:) modifier to implement Text with a gradient foreground color in SwiftUI. But the text is always at the top-leading of the view. How can I put it at the center of the view?
This is my implementation:
import SwiftUI
struct GradientText: View {
var body: some View {
LinearGradient(gradient: Gradient(colors: [.blue, .red, .orange]),
startPoint: .leading, endPoint: .trailing)
.mask(Text("Hello, world!")
.font(.system(size: 40, weight: .bold, design: .rounded)))
.padding()
}
}
And how does it look like:
