harunaga
harunaga

Reputation: 375

SwiftUI Is it possible to disable the gesture on Text of UITextView implemented by UIViewRepresentable?

I made the UITextView by UIViewRepresentable and added TapGesture like below. But I want to disable the gesture only on text. Just want to activate tap() function on out of text. Is it possible to do that ? I don't know how to detect the position of texts on UIViewRepresentable and disable the gesture...

struct TextView: UIViewRepresentable {
    
    @Binding var text: String
        
    func makeUIView(context: UIViewRepresentableContext<TextView>) -> UITextView {
        let textView = UITextView()
        textView.backgroundColor = UIColor.clear
        textView.isScrollEnabled = false
        textView.text = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
        textView.font = UIFont(name: "ArialMT", size: 20)
        textView.setContentCompressionResistancePriority(.defaultLow, for: .horizontal)
        
        return textView
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
        
    }
    
}
struct TmpView9: View {
    
    @State var text: String
    
    func tap()  -> some Gesture {
        TapGesture()
            .onEnded{
                print("taped!!!!")
            }
    }
    
    var body: some View {
        VStack {
            TextView(text: $text)
                .frame(width:300, height: 300, alignment: .topLeading)
                .border(Color.red, width: 1)
                .gesture(tap())
                
        }
    }
}

Upvotes: 1

Views: 293

Answers (1)

sergey mishunin
sergey mishunin

Reputation: 11

you can write textView.isEditable = false in makeUIView but i suggest to take a look into Coordinator and UITextViewDelegate conformance methods for further

Upvotes: 0

Related Questions