delete last character UITextField - iphone

I have an UITextField and I would like that for every tap on a character, the first character is deleted. So that I just have one character in my textField every time. Moreover I would like it to display every tap in the console log.
How can I do this?

You need to implement shouldChangeCharactersInRange method in your text field delegate:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementString:(NSString *)string{
textField.text = #"";
return YES;
}
You may need to check for range and string values to cover all possible cases (like copy/paste actions). This code just sets the text field's value to the last typed character.

UITextField inherits from UIControl, so you can use the target-action mechanism that is part of the UIControl class:
[textField addTarget:self action:#selector(updateTextField) forControlEvents:UIControlEventValueChanged];
In the action method, you can replace the UITextField's text with only the last character and log that character in the console. Note that since changing the UITextField's text will again result in the "updateTextField" message being sent a second time to the target, you will need some kind of mechanism for determining whether to update or not:
- (void)updateTextField {
if(updateTextField == YES) {
updateTextField = NO;
NSString *lastChar = [textField.text substringFromIndex:[textField.text length]];
[textField setText:lastChar];
NSLog(#"%#", lastChar);
} else {
updateTextField = YES;
}
}
Or something like that anyway...

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField.text.length > 8) {
return NO;
}
return YES;
}

Related

How to set UILabel's value according to UITextField as it is typed in iPhone?

I want to set UILabel according to the UITextField as it is typed. I mean if user want to type SAMPLE and he starts typing S then the lable should be set as S, then he types A label should also be A and so on. How to achieve this?
Please share suggestions.
Thanks in advance
Simplest way to do is to make a method and connect it with UiTextfield with event UIControlEventEditingChanged which will give you the trace on every character entered in the textfield.
[self.selectedTextField addTarget:self action:#selector(enterInLabel ) forControlEvents:UIControlEventEditingChanged];
-(void)enterInLabel
{
selectedLabel.text=selectedTextField.text;
}
The delegate also works for this as #brain said. The shouldChangeCharactersInRange: method can be a little confusing but the the following I think is pretty straight forward.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// myTextField delegate has been set
if ([textField isEqual:myTextField]){
NSMutableString *txt = [NSMutableString stringWithString:textField.text];
[txt replaceCharactersInRange:range withString:string]; //this is essentially how the textfield is updated after YES is returned
previewLabel.text = txt;
}
return YES;
}
The changing of the text field is actually done after the return YES. That is the whole point of this delegate. Just for an example, if you wanted to limit a textfield to 3 characters you could do the following to stop the text field from "replacing" the text.
if (range.location > 3)
return NO;
Set your textField delegate then call its method in your viewController.m file.. like this -
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
mylabel.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
return YES;
}
This will change the text after each increment of character.
OR You can add action on your TextField
See this link - UiTextField events
Your view controller will need to implement the UITextFieldDelegate which will allow it to receive changes to the text field. In the appropriate methods of the delegate you need to set the text of the UILabel.
I have never used UITextFieldDelegate so can't provide more detail on how to use it. I would mock up a quick example and just NSLog or debug the delegate calls to see that calls you get.

Keypressed event in UITextField

I have 10 textfields, in which I could enter only one character in each textfield. After a character is entered in each textfield, the focus should move to the next one. Similarly when i delete character from a textfield by pressing the backspace or delete, i need to get the focus to the previous textfield. If I could get the keypressed event, I could do that. Right now I am not able to find any keypressed event examples.
Implement UITextFieldDelegate.
Implement the delegate methods in the protocol. You can achieve the things you wanted.
You can set the focus by using the method becomeFirstResponder to the required textfield.
Have a look at the delegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
The text field calls this method whenever the user types a new character in the text field or deletes an existing character.
So that could solve your problem.
Based on Aadhira's answer, but taking into account Kirk Woll's comment, you can generate what the latest text will be by using stringByReplacingCharactersInRange:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *value = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSLog(#"value: %#", value);
return YES;
}
Just to give you directions:
Assign tag to each text field.
Implement UITextFieldDelegate. There are all the methods you need to detect any event that takes place inside the text field. In each method you can check the tag and move focus properly.
Hint: you can use [mainView viewWithTag:XX] to quickly pick the text field you need.
Each time the text is changed you can check the text property of the text field and it will give you the answer which button was pressed.
you have to implement the UITextFieldDelegate protocol into your code and this method will tell you when you start begin editing in text field
– textFieldShouldBeginEditing:
and you can set the if condition in this method according to your requirement...
You have to use the textfieldDelegate methods.
In your textFieldShouldReturn method you have to set your responders like
if (textfield == textField1)
{
[textField2 becomeFirstResponder];
}
else if (textField == textField2)
{
[textField3 becomeFirstResponder];
}
else
{
[textField3 resignFirstResponder];
}
return YES; // as method return type is BOOL.

Format Integer with two decimal format in UITextfield

I am presenting the user with a field that displays the number keyboard and only want them to be able to enter numbers with decimal it should be only one. After decimal it is should be at least 2 digits will be there. There should be at least one digit before the decimal. Then the input can`t be 0 or 0.00. Suppose if user enter the number 123456 it is accepted then 123.56 it also accepted how can I do this one please help me.
you can make a regex for this requirement like:-
NSString *numberRegEx = #"[0-9]+[.]{0,1}+[0-9]{0,2}";
NSPredicate *numberTest = [NSPredicate predicateWithFormat:#"SELF MATCHES %#", numberRegEx];
//Valid email address
if ([numberTest evaluateWithObject:textField.text] == YES)
{
NSLog(#"correct value");
}
else
{
NSLog(#"incorrect value");
}
First, add a target for UIControlEventEditingChanged of your text field:
[textField addTarget:self action:#selector(onEdit:) forControlEvents:UIControlEventEditingChanged];
add a BOOL property to your class:
#property(nonatomic, assign) BOOL formatting;
then create an onEdit: method like this:
-(void)onEdit:(UITextField*)field {
//if you have more than one text field you can check for the one you need to format
if(!self.formatting) {
NSString* formattedText = ...//get the formatted text according to your rules
self.formatting = YES;//infinite loop prevention
field.text = formattedText;
self.formatting = NO;
}
}
You can impose all your conditions in the -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string method. Here, textField.text will be the most recent text that the user has entered. You can parse it and see whether any of your conditions are not being met. Return NO in that case.
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if(allConditionsMet)
return YES;
return NO;
}
HTH,
Akshay

Cococa Touch - Verifying each character inputed

I want to have the user type in something. And I want to verify each character inputed. How could I do this? I want it to be in real time. So as its typed its being verified and color coded.
You could compare what I want to do to an programmers IDE how it checks the syntax as you type it.
Any help is appreciated!
Connect the UITextField delegate to the app delegate and do something like:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([string isEqual:#"b"]) {
// User typed 'b'
// Insert string with specific color
// [...]
} else {
// User typed something else
// Insert string without specific color
textField.text = [textField.text stringByReplacingCharactersInRange:range withString:string];
}
return YES;
}

Solution for iPhone new file dialog keyboard

i want to let the user type in the name of a new file, so there are certain characters i want to prevent entry on. is there a special keyboard i can use or can i disable certain keys on the iphones keyboard.
is the answer to just run a regular expression on the input text and tell the user the filename is invalid (if so what would that regular expression be?)
ANSWER: (or what i ended up doing)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
BOOL valid;
//if the user has put in a space at the beginning
if ([string isEqualToString:#" "]){
if (range.location == 0){
valid = NO;
}
else{
valid = YES;
}
}
//otherwise test for alpha numeric
else{
NSCharacterSet *alphaSet = [NSCharacterSet alphanumericCharacterSet];
valid = [[string stringByTrimmingCharactersInSet:alphaSet] isEqualToString:#""];
}
//print the warning label
if (valid == NO){
[errorLabel setText:#"Invalid input"];
}
else{
[errorLabel setText:nil];
}
return valid;
}
You can implement the delegate method
For UITextField,
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
For UITextview
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
and decide weather to append the entered characters or not.
You can implement the UITextFieldDelegate protocol and use textField:shouldChangeCharactersInRange:replacementString: to watch the text entry and prevent unwanted characters by returning NO.