Is there any way of using a gradient as foregroundColor of Text in SwiftUI?
Thanks for the answers in advance!
I guess that should help. Works with text, images and any other views.
import SwiftUI
// MARK: - API
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension View {
public func foreground(_ overlay: Overlay) -> some View {
_CustomForeground(overlay: overlay, for: self)
}
}
// MARK: - Implementation
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
private struct _CustomForeground: View {
let content: Content
let overlay: Overlay
internal init(overlay: Overlay, for content: Content) {
self.content = content
self.overlay = overlay
}
var body: some View {
content.overlay(overlay).mask(content)
}
}
Personaly I like that approach the most. But also you can combine it into:
import SwiftUI
// MARK: - API
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
extension View {
public func foreground(_ overlay: Overlay) -> some View {
self.overlay(overlay).mask(self)
}
}
Usage example