Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 528
Learning SwiftUI Avatar asked Dec 21 '25 09:12

Learning SwiftUI


1 Answers

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

like image 89
Tom Avatar answered Dec 24 '25 09:12

Tom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!