In Swift 5.5 SwiftUI it’s now possible to have links in markdown text. Is it possible to use this to add a tap handler for subtext in a Text
view? For example, I’m imagining doing something like the following, but I haven’t figured out how to construct a link that would work for this (I just get URL errors each time). Would setting up some kind of custom url schema work? Is there a better solution that doing that, like something I can add to the Text
view that acts similar to UITextView
‘s shouldInteractWith
? The goal is to solve similar problems to what is mentioned here but without having to fall back to UITextView or non-wrapping HStacks or GeometryReaders with ZStacks.
let baseText = "apple banana pear orange lemon"
let clickableText = baseText.split(separator: " ")
.map{ "[($0)](domainThatImAllowedToCapture.../($0))" }
Text(.init(clickableText)).onOpenLink { link in
print(link.split(separator: "/").last) // prints "pear" if word pear is tapped.
}
2
Answers
Using the method described by @workingdog, I've cleaned this up into the following working solution:
you could try something like this example code. It loops over your
baseText
, creates the appropriate links, and when the link is tapped/actioned you can put some more code to deal with it.