Filtering characters entered into a UITextField

前端 未结 7 1040
猫巷女王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:17

    How about this?

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

    Note:

    I am making all characters to lower case [string lowercaseString] so that you don't need to write in regex for captial/small letters.

提交回复
热议问题