How can you add clickable text in a SwiftUI Text() View?

303 views Asked by At

I've seen people using AttributedString and .link to link to something, but I need it to run a function, not open a browser link. I want something like this in SwiftUI, with wrapping, etc. Terms of service agreement (this image was made with HTML)

I tried buttons, but that results in

HStack {
   Text("By clicking continue you agree to our ")
   Button("Terms Of Service") {}
   Text(" and our ")
   Button("Privacy Policy") {}
}

Awful Looking TOS notification:

enter image description here

1

There are 1 answers

2
Tom On

Just use Text

struct ContentView: View {
@State var text: String = ""
@State var textTapped: String = ""
var body: some View {
    VStack {
        HStack(spacing: 0){
            Text("Just text ")
            Text("tap me").onTapGesture {
                tapped()
            }
            Text(" some more text")
        }
        Text(textTapped)
    }
}

func tapped() {
    textTapped = "was tapped"
}

}

enter image description here