0

How can I detect backspace in UITextView in Swift.

I have seen many solutions regarding UITextField but could not find any solution with regard to UITextView

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Bilal Awan
  • 4,730
  • 1
  • 8
  • 15
  • It's the same as `UITextField`, whatever you do in `UITextField` to detect it you can also apply to `UITextView` – Tj3n Feb 07 '18 at 11:03

1 Answers1

0

UITextViewDelegate method will help you to detect backspace

func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {

        if text == "" && range.length > 0 { //Backspace


        }
        return true
    }
Priya
  • 735
  • 5
  • 10
  • This detects deletion, it does not really detect backspace. You can't be sure this operation is not done by selection and cut. – Sulthan Feb 07 '18 at 11:11
  • that is not correct at all; it is also `1` when the user _replaces_ a character in the 1 character long selected text, without even thinking about tapping the _backspace_ button (like e.g. select 1 character and replace it with a different one); and the user also can tap the _backspace_ button to delete more than 1 character (I can delete e.g. 5 at the same time and this concept will fail)... so altogether that is a wrong solution for the original problem in general – and it does not detect any deletion at all for sure. – holex Feb 07 '18 at 11:19