Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to show a link in a text using a variable in SwiftUI?

When I use a link within a text like this:

Text("https://www.youtube.com")

As expected, the link shows up and I can press it and go to YouTube.

However, when I declare a variable first:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@State var link: String = "https://www.youtube.com"

// ...

Text(link)

The text is black and I can’t press it to navigate to YouTube.

How can I make this text tappable as well, without using a link or url?

>Solution :

The difference is LocalizedStringKey which is the default for a Text which you are setting as a String in your @State.

You have to convert in one of 2 ways

At the @State level

@State var link: LocalizedStringKey = "https://www.youtube.com"

or in the body

Text(LocalizedStringKey(link))

Whichever is best is up to you, likely option 2.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading