1

I want to add masked and unmasked characters, both together in UITextfield.

e.g I have a UITextField called UserName "TestUser". Now I want to display username in the UITextField like "****User". Is it possible to add both text together? I want to do this thing while entering character in UITextfield.

Dishant
  • 917
  • 2
  • 8
  • 25
  • Well, how about extending the UITextfield class, create a new private String with the unencrypted text, make a new get for the unencrypted string and override the setter to set the unencrypetd to your private String and the encrypted to the normal String – Xavjer May 18 '15 at 11:52
  • Is there is any specific set or length of letters you want to mask? – Bista May 18 '15 at 11:53
  • @the_UB yes... first four characters to be masked.. rest of the characters to be unmasked.. – Dishant May 18 '15 at 11:54
  • [This might be helpful](http://stackoverflow.com/questions/5223701/replacing-one-character-in-a-string-in-objective-c), you have to set range (0-3) and replace it with '*'. One more question : Do you want to replace it while user to typing? – Bista May 18 '15 at 11:56
  • @the_UB , yup.. I want to replace it while user is typing.... in above solution.. we are actually replacing the character with other character.. I don't want to replace my actual string.. I just want to mask it.. – Dishant May 18 '15 at 12:03
  • @Dishant UITextField.secureTextEntry = YES if you setting this line it again start to create textFiled secure form first character. so i think this is not possible. – Ashok Londhe May 18 '15 at 12:48

3 Answers3

1

It might be worth your while to look at the text field delegate method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string

It is called

"whenever the user types a new character in the text field or deletes an existing character"

So you can set the range 0-3 or whatever you like to "*" but you should hold the first characters somewhere else, which I believe to be possible before returning yes, with the textField parameter provided. I don't know if you tried this already or not and I haven't tried this myself, but let me know how it goes.

Kerem
  • 77
  • 9
0

something I figure out and made below solution but still it is not exactly that I want. I want to mask/asterisks first four characters while entering values in UITextField. But here I am doing this thing on textFieldShouldReturn method.

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    // Store orignal string - we can use later
    _strOriginalString = textField.text;

    //Make temporary Masked String
    NSRange replacementRange = NSMakeRange(0, 4);
    _strMaskString = [NSMutableString new];

    for (int i = 0; i<4; i++) {
        [_strMaskString appendString:@"●"];
    }

    //Replace Orignal String's Range with Masked string
    self.txtUsername.text = [_strOriginalString stringByReplacingCharactersInRange:replacementRange withString:_strMaskString];

    NSLog(@"Textfield string - %@",self.txtUsername.text);
    NSLog(@"orignal string  - %@",_strOriginalString);
    NSLog(@"masked string - %@",_strMaskString);

    return NO;
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Dishant
  • 917
  • 2
  • 8
  • 25
  • This isn't really an answer to your question. You should update your question with this information. – rmaddy May 18 '15 at 17:46
-2

I don't think, that this is possible. Why do you want to show the name like "****User" in the UITextfield? You can use a label and mask the first characters with *