关于判断输入框为空和出入空格的方法

测试代码,不输入和输入空格结果

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    textFi = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 100, 50)];
    textFi.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:textFi];
    
    UIButton *buttonN = [UIButton buttonWithType:UIButtonTypeCustom];
    buttonN.frame = CGRectMake(100, 300, 100, 100);
    [buttonN setBackgroundColor:[UIColor cyanColor]];
    [buttonN setTitle:@"点击" forState:UIControlStateNormal];
    [buttonN addTarget:self action:@selector(butonClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:buttonN];
    
}
-(void)butonClick:(UIButton *)butn
{
    //NSLog(@"不输入文字时和输入空格时显示结果....%@",textFi.text);//不输入和输入空格显示的都是空,但是不输入时textFi.text.length=0且内容为空,输入空格时textFi.text.length不等于0且内容不为空
    if ([textFi.text isEqualToString:@""])
    {
        NSLog(@"输入的内容为空。。。");
    }
    else
    {
        NSLog(@"输入的内容不为空");
    }
    
}


iOS判断字符串内容是否全部为 空格

//判断内容是否全部为空格  yes 全部为空格  no 不是
+ (BOOL) isEmpty:(NSString *) str {
    
    if (!str) {
        return true;
    } else {
        //A character set containing only the whitespace characters space (U+0020) and tab (U+0009) and the newline and nextline characters (U+000A–U+000D, U+0085).
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        //Returns a new string made by removing from both ends of the receiver characters contained in a given character set.
        NSString *trimedString = [str stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            return true;
        } else {
            return false;
        }
    }
}

你可能感兴趣的:(空格,输入框,UITextField)