0

I'm implementing an app where the user is able to insert UIImageViews on top of the UITextView view. I also insert some characters beneath the UIImageViews to help with removing them via backspace.

However, the problem is that when tested on a device, the backspace deletion speeds up with time, and starts erasing entire words and then lines, which makes the cursor skip my special characters and leave my UIImageViews on display !

There are many ways to detect the backspace call, and that's not the issue. However, the "speed up" is implemented via private methods, which makes intercepting, yet alone detecting it next to impossible !

I've been struggling with this issue for quite some time now, and normally have exhausted all the hacks around the web, with no luck :\

(tried everything from detecting keyboard characters right down to dynamic subclassing !!)

Priest
  • 242
  • 4
  • 11

1 Answers1

0

You need to make your controller a delegate of the UITextView, and implement – textView:shouldChangeTextInRange:replacementText:. You will see the delete as a message with a non-0 length textInRange and a zero length replacement text range. Just return NO for however long you want to wait.

David H
  • 40,852
  • 12
  • 92
  • 138
  • I am doing it currently, using this if statement: if (range.length == 1 && [text length] == 0) But I don't see how this could control the SPEED of the backspace – Priest Jun 06 '13 at 15:57
  • 1
    You cannot control the speed, but you can ignore them if they come in too quickly. Why do you care if they come in quickly? There is no real UI answer to this - even if you could slow it down, would you just make the delete key "dead"? What I did in a similar situation was to flash (animate) the background to Yellow, meaning, this makes no sense and I can't go back anymore. – David H Jun 06 '13 at 17:40
  • As I've mentioned, I'm inserting UIImageViews on top of the UITextView. Underneath the UIImageView I have some special text characters, that I use to detect that the cursor has hit the location of the UIImageView and thus remove it. When the backspaces speed up the code breaks to detect that the special char is removed and I'm left with UIImageViews hanging on the screen. – Priest Jun 06 '13 at 17:59
  • Everytime the user hits the backspace (which you detect in the delegate method), dispatch a block to the main queue that does a sleep for 100 milliseconds or so - that will generate UI delays. – David H Jun 06 '13 at 18:31
  • Still no luck with this... dispatch_async(myQueue, ^{ [NSThread sleepForTimeInterval:0.3]; }); Slows the UI update as you've mentioned, but does not stop the backspace from deleting entire lines. It might be time to reconsider my entire design... – Priest Jun 08 '13 at 02:41
  • You have to dispatch a block to the main queue and use usleep to force a UI slowdown. That with rate limiting in the delegate callback - ie only allow one backspace through every 500 ms - should work. – David H Jun 08 '13 at 11:22