【问题标题】:Remove the last character typed in UItextfield删除在 UItextfield 中键入的最后一个字符
【发布时间】:2012-03-23 11:42:25
【问题描述】:

如果文本字段长度超过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;
}

但它只是删除了整个文本。

【问题讨论】:

  • 你能解释一下想要的结果吗?如果文本字段包含 110 个字符怎么办?如果文本字段包含 99 个字符,用户从剪贴板粘贴 10 个字符怎么办?另外,如果最后一个字符是a,并且用户在字符串末尾添加了b,您要删除a 还是b
  • 如果你的问题解决了就接受答案,人们已经浪费了他们的时间来解决你的错误,尊重它

标签: iphone objective-c ios ipad uitextfield


【解决方案1】:

因为这一行,你删除了整个文本:

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

您想要做的更可能是以下内容:

NSString *text = textField.text;

【讨论】:

    【解决方案2】:
    - (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;
    
     }
    

    粘贴代码可能会解决您的问题

    【讨论】:

      【解决方案3】:

      在 iOS 5 或更高版本中:

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

      【讨论】:

        【解决方案4】:

        试试下面的代码

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

        【讨论】:

          【解决方案5】:

          试试这个:

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

          【讨论】:

          • @sch textField.text=[textField.text substringToIndex:[textField.text length]-1]
          【解决方案6】:

          为了修复UITextField 长度,您不必使用字符串连接。只需使用这个简单的代码:

          -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
          
              return !([string length]+[textField.text length] > FIXED_LENGTH);
          
          }
          

          这样你,作为UITextFieldDelegate,说:“如果所有字符的总和不超过最大长度,请将UITextField 附加string 更改为textField.text

          【讨论】:

            【解决方案7】:

            这是一个如何删除 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: "")
            }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2010-11-27
              • 2021-01-12
              • 2011-12-15
              相关资源
              最近更新 更多