美文网首页
UITextField 禁止输入空格判断

UITextField 禁止输入空格判断

作者: zero_zql | 来源:发表于2017-12-29 11:40 被阅读17次

    UITextField 禁止输入空格判断

    方法一:添加代理

    - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
    
    {
    NSString *tem = [[string componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]componentsJoinedByString:@""];
    
    if (![string isEqualToString:tem]) {
        return NO;
    }
    return YES;
    }
    

    方法二:

    [self.searchTextField addTarget:self action:@selector(searchTextFieldChange:) forControlEvents:UIControlEventEditingChanged];
    

    在searchTextFieldChange方法中 直接替换空的字符串

    - (void)searchTextFieldChange:(UITextField *)textField{
    
     textField.text =[textField.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
     }
    

    相关文章

      网友评论

          本文标题:UITextField 禁止输入空格判断

          本文链接:https://www.haomeiwen.com/subject/fgdigxtx.html