|

How to Create Hyperlink in SwiftUI Text

Navigating the digital landscape, hyperlinks have always been the bridges connecting one piece of content to another. They’re fundamental to the user experience. With the introduction of SwiftUI and its support for markdown in text views, creating hyperlinks has never been more seamless.

In this post, we’ll uncover how to embed hyperlinks within SwiftUI text using the markdown language.

Understanding Markdown

Before diving in, it’s essential to grasp what markdown is. Markdown is a lightweight markup language that you can use to add formatting elements to plaintext text documents. It’s both simple and readable.

SwiftUI and Markdown

Starting with iOS 15, SwiftUI introduced native support for markdown in Text views. This means you can now format text with markdown’s concise syntax directly within SwiftUI, without the need for cumbersome modifiers or external libraries.

Create a Hyperlink

Here’s how to create a hyperlink using markdown in SwiftUI:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("[Coding with Rashid](https://www.codingwithrashid.com)")
    }
}

In this code:

  • The square brackets [] contain the clickable text, in this case, “Coding with Rashid”.
  • The parentheses () contain the URL where the link will navigate to, in this case, my website.

When rendered, “Coding with Rashid” appears as a clickable link, directing users to the specified URL when tapped.

swiftui hyperlink

Combine Text with Hyperlink

You can add other text with the hyperlink as given below.

import SwiftUI

struct ContentView: View {
    var body: some View {
        Text("Visit the [Coding with Rashid](https://www.codingwithrashid.com) website for more details.")
    }
}
swiftui text with hyperlink

Hyperlinks serve as crucial connectors in the digital realm. With SwiftUI’s native markdown support, integrating hyperlinks into your app has become more straightforward.

While SwiftUI’s markdown capabilities might seem limited currently, the simplicity and readability it offers are undeniable assets. As SwiftUI continues to evolve, we can anticipate even richer markdown functionalities in the future.


Similar Posts

Leave a Reply