Skip to content

Instantly share code, notes, and snippets.

@devahmedshendy
Last active April 25, 2024 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devahmedshendy/c548f4d8cd5dbf614350abe8fa5a2c87 to your computer and use it in GitHub Desktop.
Save devahmedshendy/c548f4d8cd5dbf614350abe8fa5a2c87 to your computer and use it in GitHub Desktop.
SwiftUI View: add image as a foreground for Text view
struct ContentView: View {
private let imageUrl: String = "https://picsum.photos/seed/picsum/200/300"
var body: some View {
text
.overlay(
AsyncImage(url: .init(string: imageUrl)) { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
} placeholder: {
EmptyView()
}
)
.mask(text)
}
var text: some View {
Text("Hello")
.font(.system(size: 66))
.fontWeight(.bold)
}
}
final class HostingController: NSViewController {
override func loadView() {
super.loadView()
let controller = NSHostingController(rootView: ContentView())
view = controller.view
}
}
@_cdecl("getView")
public func getView() -> NSViewController {
return HostingController()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment