UITextView disabling text selection

Viewed 51566

I'm having a hard time getting the UITextView to disable the selecting of the text.

I've tried:

canCancelContentTouches = YES;

I've tried subclassing and overwriting:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender   

(But that gets called only After the selection)

- (BOOL)touchesShouldCancelInContentView:(UIView *)view;  

(I don't see that getting fired at all)

- (BOOL)touchesShouldBegin:(NSSet *)touches
                 withEvent:(UIEvent *)event
             inContentView:(UIView *)view; 

(I don't see that getting fired either)

What am I missing?

11 Answers

Swift 4, Xcode 10

This solution will

  • disable highlighting
  • enable tapping links
  • allow scrolling

Make sure you set the delegate to YourViewController

yourTextView.delegate = yourViewControllerInstance

Then

extension YourViewController: UITextViewDelegate {

    func textViewDidChangeSelection(_ textView: UITextView) {
        if #available(iOS 13, *) {
            textView.selectedTextRange = nil
        } else {
            view.endEditing(true)
        }
    }

}

Swift 4, Xcode 10:

If you want to make it so the user isn't able to select or edit the text.

This makes it so it can not be edited:

textView.isEditable = false

This disables all user interaction:

textView.isUserInteractionEnabled = false

This makes it so that you can't select it. Meaning it will not show the edit or paste options. I think this is what you are looking for.

textView.isSelectable = false

For swift, there is a property called "isSelectable" and its by default assign to true

you can use it as follow:

textView.isSelectable = false
Related