Ross Knipe
Ross Knipe

Reputation: 519

objective c move cursor back after textField manipulation

I have a UITextField for the input of multiple twitter hashtags, each separated by a space. Using this code, when you press the space bar a hash # is automatically added to speed up hashtag input.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string 
{
if([string isEqualToString:@" "])
  { 
    NSString *text = _hashtagTextView.text;
    text = [text stringByAppendingString:@" #"];
    _hashtagTextView.text = text;
  }
  return YES;
 }
}

The problem is that this code adds a space after the # and so you need to press backspace to make the hashtag, I’m not sure why.

How can I programatically move the cursor back 1 character?

Upvotes: 1

Views: 71

Answers (1)

MichaelV
MichaelV

Reputation: 1261

Take a look on your code one more time: text field asking for your permission to change some text (add to end in particular). If new fragment is whitespace, your appending to your text ‘ #’, and granting permission to add whitespace. Perahaps you want to forbide adding whitespace If yor had inserted ‘ #’ already

Upvotes: 1

Related Questions