views:

178

answers:

1

Hi All

I really want to be able to detect a paste event in a UITextView, however it appears this cannot be done.

I originally tried subclassing a UITextView and overriding the paste: method, but it never gets called on a paste event.

Has anyone been able to do this? A previous question on the same ilk didn't have an answer back in August...

+1  A: 

The text view doesn't catch the paste: event because it wasn't the actual responder is not the text view, but the private web view (UIWebDocumentView) that powers the text view.

However, on paste, the web view will call the text view's (private) -[UITextView keyboardInput:shouldInsertText:isMarkedText:], and in turn, the text view's delegate's -textView:shouldChangeTextInRange:replacementText:.

Therefore, you just need to implement -textView:shouldChangeTextInRange:replacementText: in the text view's delegate.

(Of course, normal keyboard input will trigger this method too. There's no perfect way to distinguish them.)

KennyTM