开发者

Replace a given string with an other on UITextView

On my app I need to do this: when a character is typed on TextView to be saved on a NSString and after that to be replace with '*'. I tried this :开发者_JS百科

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSLog(@"typing...");
    text=@"*";
    passwordText=textView.text;
    NSLog(@"password %@",passwordText);

    NSString* nextText = [textView.text stringByReplacingCharactersInRange:range withString:text];
    textView.text=nextText;
    NSLog(@"next %@",nextText);
    NSLog(@"textview.text %@",textView.text);

    return YES;
}

where passwordText is the NSString in which I want to save the text introduce from keyboard on UITextView.

The result is this : http://i54.tinypic.com/2cx9ueo.png (here I introduced 'we' and I see this :'*w*e'. Can anyone help me to solve this?

I mention that I must do this using UITextView, and not UITextField.


I can tell you why you get character along with the *, though i am not sure whether your approach is worth to go through this.

make your return statement as NO, this will discard the new key pressed. The YES is currently placing that character next to your programmatic '*'.


Just return a NO in the method if you want the change to be immediate. If you want it to be a little delayed (i.e. first show a character then replace with * like in password fields), return a YES and run another method from the

textView:shouldChangeTextInRange:replacementText: method to be fired after 0.5 seconds (or another number if you like) using a timer.

This new method can replace the last added character or changed character with a *.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜