Skip to content
DeveloperMemos

Creating Hyperlinks in Text with SwiftUI

SwiftUI, iOS, Hyperlinks1 min read

So you want to add hyperlinks to you Text view in SwiftUI? Here's a quick post highlighting a couple of ways you can achieve this!

Using Markdown

SwiftUI supports Markdown(markdown inside Text is available on iOS 15 and above), allowing you to easily embed links in text. Here’s how you can do it:

1import SwiftUI
2
3struct ContentView: View {
4 var body: some View {
5 Text("Check out our [Terms of Service](https://example.com/tos)")
6 }
7}

This code creates a text view with a hyperlink to the specified URL.

Using Link View

The Link view is another straightforward way to create hyperlinks in SwiftUI:

1import SwiftUI
2
3struct ContentView: View {
4 var body: some View {
5 Link("Policy", destination: URL(string: "https://example.com/privacy")!)
6 }
7}

This example creates a link to the privacy policy page. Remember to safely unwrap URLs in production code.

Customizing Hyperlink Appearance

Customize the appearance of hyperlinks by using view modifiers:

1import SwiftUI
2
3struct ContentView: View {
4 var body: some View {
5 Text("Visit our [Website](https://example.com)")
6 .foregroundColor(.blue)
7 .underline()
8 }
9}

Here, the hyperlink is styled with blue color and underlined.