I would like to display a phone number in a SwiftUI Text (or any View), and then make it clickable so that it will open the 'Phone'.
Is there a way to do this with SwiftUI, or should I try to wrap a UITextView in SwiftUI and do it the old-fashioned way with NSAttributed string etc?
I've read the documentation for Text in SwiftUI, and couldn't find anything about how to do this. Currently trying to do this in Xcode 11 beta 5.
I've searched 'text' in the SwiftUI API in SwiftUI.h
I've also searched stackoverflow [swiftui] and google with queries like "make phone number/url tappable", "Tappable link/url swiftUI" etc..
Text("123-456-7890") .onTapGesture { // do something here }
(text will be Japanese phone number)
To create a link, enclose the link text in brackets (e.g., [Duck Duck Go]) and then follow it immediately with the URL in parentheses (e.g., (https://duckduckgo.com)).
Using iOS 14 / Xcode 12.0 beta 5
Use new link feature in SwiftUI for phone and email links.
// Link that will open Safari on iOS devices Link("Apple", destination: URL(string: "https://www.apple.com")!) // Clickable telphone number Link("(800)555-1212", destination: URL(string: "tel:8005551212")!) // Clickable Email Address Link("[email protected]", destination: URL(string: "mailto:[email protected]")!)
Try this,
let strNumber = "123-456-7890" Button(action: { let tel = "tel://" let formattedString = tel + strNumber guard let url = URL(string: formattedString) else { return } UIApplication.shared.open(url) }) { Text("123-456-7890") }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With