iPhone restrict user from entering space in textfield

前端 未结 6 1971
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 08:40

i want to restrict user from entering space in a UITextField. for this i m using this code

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


        
6条回答
  •  生来不讨喜
    2021-01-05 09:30

    Below is what I am using for password and confirm password

    In PrefixHeader.pch file add below

    #define NONACCEPTABLE_PASSWORD_CHARACTERS @" "
    

    And in code use below.

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {
    
    
        if (textField==passwordTF || textField==confirmPasswordTF) {
            NSCharacterSet *cs = [NSCharacterSet characterSetWithCharactersInString:NONACCEPTABLE_PASSWORD_CHARACTERS];
    
            NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
    
            return [string isEqualToString:filtered];
        }
    
        return YES;
    }
    

提交回复
热议问题