tags:

views:

220

answers:

3

Hi dear ,i have problem with the custom backspace and enter buttons on custom iPhone keyboard ,

The Backspace : my codes just remove characters at the end of the line not from the cursor location .

if ([textView.text length]>0) textView.text = [textView.text substringToIndex:([textView.text length]-1)];

and read this question http://stackoverflow.com/questions/2536691/custom-keyboard-iphone-having-problem-with-backspace-button-in-uitextview but didn't solve my problem .

The Enter : i have same problem like backspace the enter button just the inserts new line at the end of sentence not not from the cursor location .

textView.text = [NSString stringWithFormat:@"%@\n", textView.text];

how should i change my codes to work fine ? thank you

EDIT : //BACKSPACE BUTTON CODE :

NSRange deleteRange = textPad.selectedRange;
deleteRange.length -= 1;
if ([textPad.text length]>0) textPad.text = [textPad.text stringByReplacingCharactersInRange:deleteRange withString:@""];
+1  A: 

You can use the UITextView selectedRange method to determine the beginning and end of the selected text, and delete from that range instead of from the end of the string.

hotpaw2
sorry iam new to iPhone sdk , can you show me the example ?
Mc.Lover
would you plz heeeeeeeeeeeelmp me?:( i really need the answer
Mc.Lover
+3  A: 
textView.text = [textView.text stringByReplacingCharactersInRange:textview.selectedRange withString:@"\n"]; // Replace the selected characters with a new line

or

textView.text = [textView.text stringByReplacingCharactersInRange:textview.selectedRange withString:@""]; // Delete the selected character
JustSid
thank you very very much , sorry iam new to iphone sdk , about the backspace i wanna delete just one character on any range . your code just delete the selected text not 1 character .
Mc.Lover
would you please help me again with length -1 ?
Mc.Lover
Sorry, wasn't online the last hours. Okay, here is the code to delete only length minus one character:NSRange deleteRange = textview.selectedRange;deleteRange.length -= 1; // This will shrink the range from its original length to length - 1. You can set this to whatever you want.textView.text = [textView.text stringByReplacingCharactersInRange:deleteRange withString:@""]; // Instead of deleting the selected ones, delete the characters from the new range`Edit: No Code tags or comments?! FFFFUUUUU D:
JustSid
thank you , wasn't online for hours :D , i use your code but compiler gave me SIGBART error and app crashed !! i edit my question and wrote the code which you suggested
Mc.Lover
+2  A: 

here is the best iphone custom backspace code :D :

  NSRange deleteRange = textPad.selectedRange;

        if (deleteRange.length >0)
        textPad.text = [textPad.text stringByReplacingCharactersInRange:deleteRange withString:@""];


        else

            if (deleteRange.location > 0)
            textPad.text =  [textPad.text  stringByReplacingCharactersInRange:NSMakeRange(deleteRange.location-1,1) 
 withString:@""];

deleteRange.location--;
deleteRange.length = 0;
textPad.selectedRange = deleteRange;
Momeks
GREAAAAAAAT , it worked awesome !
Mc.Lover