簡體   English   中英

刪除在UItextfield中鍵入的最后一個字符

[英]Remove the last character typed in UItextfield

如果文本字段長度超過100,我需要刪除最后一個字符,我使用以下代碼:

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

但它只是刪除了整個文本。

您可以刪除整行文本:

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

您想要做的更有可能是:

NSString *text = textField.text;

在iOS 5或更高版本中:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if([textField.text length] >= 100)
    {
        [textField deleteBackward];
    }
}
- (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;

 }

粘貼代碼可能會解決您的問題

嘗試使用以下代碼

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

為了修復UITextField長度,您不必使用字符串連接。 只需使用這個簡單的代碼:

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

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

}

這樣你就像UITextFieldDelegate一樣說:“ 只有當所有字符的總和不超過最大長度時, UITextField附加string更改為textField.text

以下是如何刪除UITextField中的任何文本的示例; 適用於iOS 10.3及更高版本

@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: "")
}

試試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM