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
Related
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 *
I have a text field which the user enters a number and it gets converted to an integer.
The keyboard set is to decimal numerical one. Except the paste function is still enabled so the user can paste text into it.
I want to set it to change a label to #"error" if the user enters a non numerical value into it.
I do not want to disable the paste function as I still want the user to have the ability to copy and paste their own numerical values into my app.
Simply disallowing entry of non-numeric characters is one way. But what you asked is how do you detect non-numerics...
Use
NSRange range = [myTextField.text rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]];
if(range.location == NSNotFound) {
// then it is numeric only
}
If you want apply restriction that use have to only enter numeric value then use delegate method of UITextField:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSCharacterSet *nonNumberSet = [[NSCharacterSet decimalDigitCharacterSet] invertedSet];
return ([string stringByTrimmingCharactersInSet:nonNumberSet].length > 0) || [string isEqualToString:#""];
}
This will returns YES if the string is numeric, otherwise return NO.
EDITE:
#Adam's answer is the best for check text is only numeric or not.
NSRange range = [myTextField.text rangeOfCharacterFromSet:[NSCharacterSet letterCharacterSet]];
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.
I have a table row which has some data entry by using UITextField.
Lets say I have some data in it "my data"
Next time when I come back to this activity, I want to edit it and append with . using double space keyboard shortcut to get the result as "my data."
The issue is when I use double space shortvut it overwrite it and replace the value of UITextfield with ". "
Is there any way to append this shortcut.
I hope I explain my problem correctly.
Well after lot of digging and some manipulation I have following answer, this could help if some one has the same issue.
use following delegate call
(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
and copy the following code to fix the issue described above.
//Check for double space, hot key shortcut provided on keyboard.
if((range.location > 0 &&
[string length] > 0 &&
[[NSCharacterSet whitespaceCharacterSet] characterIsMember:[string characterAtIndex:0]] &&
[[NSCharacterSet whitespaceCharacterSet] characterIsMember:[[textField text] characterAtIndex:range.location - 1]])) {
//Manually replace the space with your own space, programmatically
NSRange backward = NSMakeRange(range.location - 1, 1);
textField.text = [rightTextField.text stringByReplacingCharactersInRange:backward withString:#". "];
return NO;
}
This will append . to the existing data in UITextField.
Still looking for better answer.
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.