Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make UITextView selectable in Swift

I have this UITextView in Swift:

let contactText = UITextView(frame: CGRectMake(0,0,200,50))
contactText.selectable = true
contactText.dataDetectorTypes = UIDataDetectorTypes.Link
contactText.userInteractionEnabled = true
contactText.editable = false 
contactText.text = "Some text goes here and some website here www.google.com"
self.view.addSubview(contactText)

I want to be able to double tap or tap and hold to select the text and then give the user the option to select all or copy (just like most apps do). The above code didn't work although it seems like it should, is there another way of doing it?

EDIT:

The above code actually works, the reason why is not working (not 100% sure yet) is probably because the UITextView is within a UIView that has a PanGestureRecognizer attached to it, so maybe that is blocking it? any ideas?

like image 580
matt Avatar asked Dec 13 '25 09:12

matt


1 Answers

To make a UITextView selectable you only need to add these two lines:

yourTextView.selectable = true

yourTextView.userInteractionEnabled = true

like image 65
Alex Zanfir Avatar answered Dec 16 '25 09:12

Alex Zanfir