Gradient as foreground color of Text in SwiftUI

后端 未结 6 1063
终归单人心
终归单人心 2021-02-08 12:55

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

Thanks for the answers in advance!

6条回答
  •  爱一瞬间的悲伤
    2021-02-08 13:22

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

提交回复
热议问题