views:

210

answers:

1

Hey Guys check this code ( My Custom Keyboard) :

-(IBAction) updateTextBackSpace:(id)sender
{
    if([txtview.text length]>0)
    {
        NSString *deletedLastCharString = [txtview.text substringToIndex:([txtview.text length]-1)];
        [txtview setText:deletedLastCharString];
    }
    else
    {
        return nil;
    }
}  

The thing is that I can't figure out how to change this code so that , I can erase any text in any give line at the cursor, the backspace starts to erase from end of the line .I should be able to erase(backspace) from the cursor location Please Help

+3  A: 

replace this

NSString *deletedLastCharString = [txtview.text substringToIndex:([txtview.text length]-1)];

with

NSRange range  = [txtview selectedRange];
NSString *deletedLastCharString = [txtview.text substringToIndex:([range.location]-1)];
mihirpmehta
The code looks good .. But I am getting an error : Expected ':' before ']' token . Sorry for the noobness
Hey I edited that to (range.location) instead of [range.location] .It worked but it is clearing the text which 'after' the cursor also.If i wanted edit ABCD EFG and i place cursor at D and hit the backspace it's eating the remaining EFG so the output is :ABC where I want as : ABC EFG .Thanks Mihir.
try using [txtview.text substringToIndex:([txtview.text length] - [range.location]-1)];It should work...
mihirpmehta
Tried the above . It gives error as Expected ':' before ']' token .and when I change [range.location] in your code to (range.location)then the code runs but crashes.Thanks for helping . What should I do ?
Previous one was right... but there is some logical error... please put some more code...
mihirpmehta
did you solve your problem ? because i still have some errors !!! like you and doesn't work . :(
Momeks
any help???????
Mc.Lover