UITextfield format ****user - ios

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.

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.

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;
}

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 *

Related

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?
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

How to count the number of characters in a text field dynamically in IOS

I want to be able to calculate 200 - the number of characters at any time.
Meaning that I want to display the number of characters and have it update any time the user inputs or deletes characters.
The code I have properly counts 200 - the number of characters at any time but I don't know how to update the text field constantly.
-(void)updateLabel{
// Get the text from the message
// find the length of the text
// subtract from the converted number in the charCounter Label
// display in the charCounter Label
int length = _message.text.length;
int charLeft = 200 - length;
NSString* charCountStr = [NSString stringWithFormat:#"%i", charLeft];
_charCounter.text = charCountStr;
}
And then I call the updateLabel function in the viewDidLoad.
I suspect that there must be some other function to persistently update the viewController
Just implement the UITextFieldDelegate method textField:shouldChangeCharactersInRange:replacementString: and call your updateLabel method from within it.
From the docs:
The text field calls this method whenever the user types a new character in the text field or deletes an existing character.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
[self updateLabel];
return YES;
}
Don't forget to set your text field's delegate property, and to make sure that your class conforms to UITextFieldDelegate.
It seems that you want a Twitter-like dynamic count of the amount of characters that a user can type into a UITextBox. So you have a limit of 200 characters and you want to inform the user how much characters he or she typed dynamically.
If you want that method to run every time the the text changes:
// textField is the UITextField you type your text in to.
// this could be a reference to that field via an IBOutlet or you created it manually
[textField addTarget:self
action:#selector(updateLabel)
forControlEvents:UIControlEventEditingChanged];

Add a permanent Character to UITextField

Is there a way to permanently add a letter to a UITextField where the user cannot delete it? I want to add a single character and the user not be able to delete it but they can still add letters after.
Cheers,
p.s. This is for iOS
A UITextField has a delegate method called should change characters in range, this method basically ask, should i add or remove the next character? and from that you can dictate what you would like. Here is some example code.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
BOOL shouldChangeCharacters = ( (textField.text.length + string.length) > 1 );
return shouldChangeCharacters;
}
This code means if the new character being added plus the current text is greater than 1 then it is okay to make the change, if the text is not greater than one, then we will not make the change...
Now, under the assumption that someone may try to paste over your character, this delegate method is still called but you have to do a few things.
if (range.location == 0) {
NSString* firstCharacter = [string substringToIndex:1];
BOOL firstCharacterIsDesiredCharacter = [firstCharacter isEqualToString:#"#"];
if ( !firstCharacterIsDesiredCharacter ) {
NSString* symbolWithText = [NSString stringWithFormat:#"#%#",text];
// ******************** \\
[textView setText:symbolWithText];
return NO;
// or we could do this
string = symbolWithText;
// ******************** \\
}
}
Both scenarios, are modifying the values of the parameters... some people don't like doing that. It could be a bad programming practice.. or if you are going to modify them there's some process you should do first.
With that, we only need to run this code if they are trying to replace the first character, i substituted the hash tag symbol, the first character is from a range of location 0 and length of 1. So if the range is equal to 0, then we run our code to fix it. This code also takes into consideration that they might be pasting the special symbol with it. so if the UITextField read #work, and they tried to copy "work" or "#work" it takes both scenarios into consideration and completely skips the code if the hash mark is the first character.
UITextField Reference
try this
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
label.text = #"left";
label.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.0];
textField.leftViewMode = UITextFieldViewModeAlways;
textField.leftView = label;
Try using this:
in your .h:
IBOutlet UITextFeild *textFeild;
in your .m:
- (BOOL)textFieldShouldReturn:(UITextField *)textFeild {
NSString *textFeildText = textFeild.text;
textFeild.text = [NSString stringWithFormat:#"string you want to always add %#",textFeildText];
return NO;
}
This won't always show up when the user is editing the text box, but once they hit return it will automatically be added to the text box. If you use this code I recommend that you do not have the string that you want in the text field by default, or else you may end up with something like this:
String you want to add String you want to add Hello World!
If you just use the code I give you and don't put the string that you want to always be in the field by default, you're good to go! If you want to make the keyboard automatically disappear after they tap return just add this in the textFeildShouldReturn method above the 'return NO' statement:
[textFeild resignFirstResponder];

Stop UITextField Secure Text From Clearing Existing Text

I have a UITextField that is a password field declared by using
passwordField.secureTextEntry = YES;
My issue is that when I type half my password, click on a different component, and come back to the passwordField and start typing again, iOS clears the existing text.
Is there a way to prevent this from happening or turn it off?
-Henry
Simplest way to do this is to implement delegate for UITextField with a code similar to this:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)replacementString {
if (textField.text) {
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:replacementString];
} else {
textField.text = replacementString;
}
return NO;
}
By default, no, you can't do it for secure text fields. You can work around it though.
Implement the delegate method textField:shouldChangeCharactersInRange:replacementString: and use the values passed to decide what to do.
i.e. if the range is the entire string and the replacement is an empty string, don't allow the change. The new text might be provided as the newly typed character, you'll need to check what parameters the method actually receives.
Well I have the same situation when I was changing keyboard type to make my password combo of 4(digits)+1(alpha), to stop clearing secureTextEntry,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
textField.keyboardType = UIKeyboardTypePhonePad;
[textField reloadInputViews];
return YES;
}
It reloads UITextField but not its text, so without clearing text it changed the keyboard from alpha to numeric vice versa. You can use it for your own functionality.

How to make both UITextFields content equal at the same time?

I have some UITextFields and one button, Can anyone tell me how to make both textfields (first Street Address field and Street Address field below Same As Above button) content equal at the same time if my button (Same As Above) is checked (I m using a boolean variable isSameAsAboveChecked on button click and making value Yes and No on respective actions).
Means If Same As Above button is checked, and i m writing something in First Street Address textfield then at the same time content of Second Street Address text field should start changing. and same for all the other fields.
Thnaks in advance.
Set both textfield.delegate =self
I just tried with :
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
self.yourSecondField.text=self.yourFirstField.text;
return YES;
}
Issue: If First field has been entered as say "XYZA" , second field has just "XYZ", maybe someone can edit this answer or give a better one.
Use this delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range
withString:string];
text2Field.text = toBeString;
return YES;
}
In it first set the entered text to other textField and then return YES
Dont forget to return yes,
for this functionality use this code
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
if (textField == street_Address) {
if (check_box) {
street_address.text=street_Address.text;
}
else
[street_Address resignFirstResponder ];}
similarly u apply for rest of other fields. First u check ur check box is enable or not. if enable then pass the value to below text fileds otherwise resign current text field.

Resources