Last active
April 25, 2024 10:56
-
-
Save devahmedshendy/c548f4d8cd5dbf614350abe8fa5a2c87 to your computer and use it in GitHub Desktop.
SwiftUI View: add image as a foreground for Text view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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