Delete the last character entered in the UItextfield

I need to delete the last character, if the text field is longer than 100, I used the following code:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textfield.text.length>=100)
    {
        NSString *text;
        textField.text=text;
        NSString *newString = [text substringToIndex:[text length]-1];
        textField.text=newString;
    }
    return YES;
}

But it just erases all the text.

+5
source share
7 answers

You delete all text because of this line:

textField.text=text; // text is nil here

What you wanted to do is more likely the following:

NSString *text = textField.text;
+5
source
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

      if(textfield.text.length>=100)
     {
         NSString *text;
         text =[[textField text] stringByAppendingString:string];
         NSString *newString = [text substringToIndex:[text length]-1];

         textField.text=newString;
     }
     return YES;

 }

Embed code may solve your problem

+3
source

 - (BOOL)textView:(UITextView *)aTextView shouldChangeTextInRange:(NSRange)aRange replacementText:(NSString *)aText {

    NSString* newText = [textView.text stringByReplacingCharactersInRange:aRange withString:aText];

    // CGSize strSize = [newText sizeWithFont:textView.font constrainedToSize:CGSizeMake(200, 10000) lineBreakMode:UILineBreakModeWordWrap];

   if([newText length] > 100)
   {
       return NO; // can't enter more text
   }
   else
      return YES; // let the textView know that it should handle the inserted text
}
+2

iOS 5 :

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([textField.text length] >= 100)
    {
        [textField deleteBackward];
    }
}
+2

:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
return !([newString length] >100);
}
0

UITextField, . :

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

    return !([string length]+[textField.text length] > FIXED_LENGTH);

}

So you, like UITextFieldDelegate, say: "change the UITextFieldaddition stringto textField.text only if the sum of all characters does not exceed the maximum length"

0
source

Here is an example of how to remove any text in a UITextField; applies to iOS 10.3 and later

@IBOutlet var upcText: UITextField!
if var textRange = upcText.selectedTextRange {
    if textRange.isEmpty && textRange.start != upcText.beginningOfDocument {
        // text has not been selected and the cursor is not at the beginnning
        // then create new range to delete previous character
        textRange = upcText.textRange(from: upcText.position(from: textRange.start, offset: -1)!, to: textRange.start)!
    }
    upcText.replace(textRange, withText: "")
}
0
source

All Articles