Knowing the complete changed string in textField:shouldChangeCharactersInRange:replacementString:

前端 未结 3 1529
执念已碎
执念已碎 2021-02-02 08:25

I just asked a question about how to monitor changes to a UITextField and received this response :

- (BOOL)textField:(UITextField *)textField should         


        
相关标签:
3条回答
  • 2021-02-02 08:27

    Swift Version

    In Swift We need to cast textField's text to NSString. The following can be useful:

    let newString = (textField.text! as NSString).replacingCharacters(in: range, with: string)
    
    0 讨论(0)
  • 2021-02-02 08:35
    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    {
        NSString * searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    //    [textField2 setText:[textField1.text stringByReplacingCharactersInRange:range withString:string]];
        NSLog(@"%@",searchStr);
        return YES;
    }
    
    0 讨论(0)
  • 2021-02-02 08:48
    NSString * proposedNewString = [[textField text] stringByReplacingCharactersInRange:range withString:replacementString];
    
    0 讨论(0)
提交回复
热议问题