August 11, 2021Set 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) {
}
}
Level up your Swift skills!$35
100+ tips to take your Swift code to the next level
Swift Gemsby Natalia Panferova
- 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
Level up your Swift skills!
100+ tips to take your Swift code to the next level
Swift Gems
by Natalia Panferova
$35If you are enjoying our blog and would like to support us, you can sponsor us on GitHub.
Subscribe to our monthly newsletter to get exclusive Swift and SwiftUI tips, project updates, behind-the-scenes insights, and special discounts on our books.
For more frequent updates, follow us on X @nilcoalescing.