Search code examples
iosuitextviewdragbackspace

How to get deleted text in UITextView?


I want to get deleted text in UITextView.

Because I have to decrease UITextView, when '\n' deleted.

However, shouldChangeTextInRange delegate method is only return last string.

So, For example

UITextView.text = @"ABCD\nEFG";

and user delete text is @"D\nEFG".

Then, returned text is not '\n'. so I do not know whether the line number should be reduced. help me!


Solution

  • You know the range of text that is going to be changed within shouldChangeTextInRange.

    Before you return YES from shouldChangeTextInRange, you can scan the characters within that range to see if there is a "\n" or "\r" character there and then you can decrease the line number by the number of new line characters found in the range.