Learn Courses My Dashboard

Hyperlink with text

How do I get a link to work in a list with words before it that’s not hyperlinked?

Text("Telephone: [Privacy Policy](https://example.com)")

except the link needs to allow me to make it dynamic… (variable).website. - For instance

I tried doing this too, but it just makes the whole thing text.

 let wb = "[Privacy Policy](https://example.com)"
                List {
                    Text("Telephone:2 " + wb)

I know about what’s below, but don’t know how to combine text with it that’s not hyperlinked

Link("Website: ", destination: URL(string: base.website)!)

You could use an HStack.

HStack(spacing: 0) {
  Text("My text")
  Link("Website: ", destination: URL(string: base.website)!)

Text can display links when you feed it an AttributedString. When you give it a static String that contains markdown, it assumes you really want an AttributedString and converts it for you.

The problem is that when you use a dynamic String (i.e., one you build on the fly) to initialize a Text, SwiftUI can’t tell if you want a String or an AttributedString. So you have to help it along.

You can do something like this:

struct MarkdownLink: View {
    let wb = "https://example.com"
    var markdownLink: AttributedString {
        try! AttributedString(markdown: "Telephone: [Privacy Policy](\(wb))")
    var body: some View {

In other words, build your AttributedString in a computed variable and feed that to Text so there’s no ambiguity about what you want.

You could even do away with the computed variable altogether by doing:

Text(try! AttributedString(markdown: "Telephone: [Privacy Policy](\(wb))"))

but I find that kind of icky.

Of course, you could improve this by changing the try! to try and adding some error handling in case the conversion to AttributedString fails, but I’ll leave that as an exercise for the reader.

I tried something similar. My problem, which I may have to fix in a different way is below. I may just have to add the tele: part into json…

 let comm = "tele:\(base.Commercial)"
    var commercialLink: AttributedString {
            try! AttributedString(markdown: "Telephone: [Privacy Policy](\(comm))")

Screenshot 2023-03-05 at 7.16.26 AM

For your similar example, I found this on stackoverflow:

 Text("[Privacy Policy](https://example.com)")

I tried your option too, but it’ll crash saying it unexpectedly found nil while unwrapping an Optional Value. (Which I don’t get cause it’s not optional in my model. And I also don’t understand why it’s claiming nil when right above that suggested code, I use that same base.website as a text field and shows fine. So it’s obviously not nil.

tele:(301) 981-3604 is not a valid telephone URL.

It should be tel:3019813604.

1 Like

base.website may not be nil but URL(string: base.website) apparently is.

Whatever base.website is, it doesn’t convert to a valid URL and so URL(string:) returns nil. So when you force unwrap it, you get that error.

1 Like

I must have misspoken, it only crashes when I attempt “tele:” + base.commercial. It does not crash with base.website because that’s just a traditional url (http://…)

Your first response tells me what I need though - will make those adjustments to json itself and then should be good. Thank you for your help Roosterboy