views:

27

answers:

1

By "delete all," I mean what happens when you have a lot of text and you hit backspace for a few seconds, and the text view clears completely. I want to disable that function for my application. How can I do this (if it's even possible/allowed)?

Thanks in advance!!

+1  A: 

Link up the delegate outlet of the text view to one of your objects, and put this in it

-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range
          replacementText:(NSString *)text
{
    if ( [text length] > 0 ) return YES; // adding = OK
    if ( range.length == 1 ) return YES; // removing one = OK
    if ( [text length] == range.length ) return NO; // remove all != OK
    return YES; // all else is ok (this includes autocorrection, cut/paste things)
}
mvds
Hmmm, this isn't working for me.
ayanonagon
You should put a line `NSLog(@"shouldChange loc %d len %d with '%@'?",range.location,range.length,text);` in this function to see if, when and how it is called. Then adjust the conditions, if needed.
mvds
They all say "shouldChange loc [some number] len 1 with ''?" Maybe it's not possible after all.
ayanonagon
Just tested, and indeed, the multi-delete action (delete-all or delete-word) doesn't pass the shouldChangeTextInRange. You *do* get notified in `-(void)textViewDidChange:(UITextView*)textView` where `textView.text` is now empty. So one hack would be to save the last non-empty string in `shouldChangeTextInRange` and put that back in `textViewDidChange`.
mvds