Quick Tip Icon
Quick Tip

Set custom actions for links in Text views

Since iOS 15 and macOS 12 SwiftUI Text views can contain interactive links created with Markdown or AttributedString.

To customize actions of such links we can use openURL environment value.

struct ContentView: View {
    var body: some View {
        Text("Visit our [website](https://example.com)")
            .environment(\.openURL, OpenURLAction { url in
                handleURL(url)
                return .handled
            })
    }
    
    func handleURL(_ url: URL) {
        // handle URL here
    }
}