I have a let txt = "Hello My name is jhon cena, call me +17878 Thank you" Now requirement is to make tappable phone number & make it bold font.
Here is my code which gives error Cannot convert value of type 'some View' to expected argument type 'Text'
struct CustomView1: View {
let phoneNumber = "+17878"
var body: some View {
Text("Hello My name is Jhon Cena, call me ") +
Text(phoneNumber)
.font(.body)
.fontWeight(.bold)
.foregroundColor(.blue)
.underline()
.gesture(TapGesture().onEnded {
self.dialNumber()
})
+ Text(". Thank you")
.font(.body)
}
func dialNumber() {
// Replace this with the actual code to open the dialer with the provided phone number
if let phoneURL = URL(string: "tel://\(phoneNumber)"), UIApplication.shared.canOpenURL(phoneURL) {
UIApplication.shared.open(phoneURL)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
CustomView1()
}
}
Check the order of modifiers you apply, and see the return type of those modifiers:
and you cannot basically append a View type and a Text type. If you want to perform an action with individual text elements, you need to put them in a
HStackinstead of adding them. It might be enough for two single line texts that won't wrap to second line.More details can be found SwiftUI tappable subtext