I have a text field with the phone number in it. It should not be edited, but the user should have an ability to select it by long pressing on the phone. When he long presses text should get selected, not just copied to the clipboard. So far I only got it to work with isUserInteractionEnabled == true but I need to still have it false. Any suggestion on how I could do this? My code now:
func longPress(_ longPressGestureRecognizer: UILongPressGestureRecognizer) {
self.cell?.textField.isUserInteractionEnabled = true
self.cell?.textField.becomeFirstResponder()
self.cell?.textField.selectedTextRange = self.cell?.textField.textRange(from: (self.cell?.textField.beginningOfDocument)!, to: (self.cell?.textField.endOfDocument)!)
//self.cell?.textField.isUserInteractionEnabled = false
}
For your purposes,
UITextFieldis not a suitable choice. As the documentation states,UITextFieldis (emphasis added by me):Hence, you cannot set a
UITextFieldto be non-editable. If you want to display text on the UI, which shouldn't be editable, use aUILabelorUITextView.You can set the
UITextView'sisEditableproperty to false and it will still be selectable as long asisSelectableis set to true.A
UILabeldisplays read only text, so you don't even need to take care of disabling editing when using it.