Filtering characters entered into a UITextField

前端 未结 7 1005
猫巷女王i
猫巷女王i 2020-12-14 03:54

I have a UITextField in my application. I\'d like to restrict the set of characters that can be can be entered into the field to a set that I have defined. I could filter th

7条回答
  •  囚心锁ツ
    2020-12-14 04:18

    This is what I use to restrict the user to uppercase A-Z. Adjust the regex variable according to taste:

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
        NSString* regex = @"[^A-Z]";
        return ([string rangeOfString: regex
                          options:NSRegularExpressionSearch].location == NSNotFound);
    };
    

提交回复
热议问题