UITextField检测输入内容不能有空格的处理

 //添加监听方法

 [textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];

- (void)textFieldChanged:(UITextField*)textField{
    //空格解决方案
    NSString *_string = textField.text;
    NSRange _range = [_string rangeOfString:@" "];
    if (_range.location != NSNotFound) {
        //有空格
       NSLog(@"密码中不能包含空格");
        textField.text = [NSString stringWithFormat:@"%@",[textField.text substringToIndex:textField.text.length - 1]];
    }
}
原文地址:https://www.cnblogs.com/zk1947/p/9443748.html