— SwiftUI, iOS, Hyperlinks — 1 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!
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 SwiftUI2
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.
Link
ViewThe Link
view is another straightforward way to create hyperlinks in SwiftUI:
1import SwiftUI2
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.
Customize the appearance of hyperlinks by using view modifiers:
1import SwiftUI2
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.