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
}
}
Check out our new book!
Swift Gems
100+ tips to take your Swift code to the next level
Swift Gems
100+ tips to take your Swift code to the next level
- Advanced Swift techniques for experienced developers bypassing basic tutorials
- Curated, actionable tips ready for immediate integration into any Swift project
- Strategies to improve code quality, structure, and performance across all platforms
- Practical Swift insights from years of development, applicable from iOS to server-side Swift