ios UITextField文本框基本使用,以及所有代理方法的作用

/*

     UITextField文本输入框

     */

    UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 275, 50)];

    //设置边框形式

    /*

     UITextBorderStyleRoundedRect 圆角形式

     UITextBorderStyleLine 线条形式

     UITextBorderStyleBezel 槽形式

     */

    textField.borderStyle = UITextBorderStyleRoundedRect;

    //通常用于寻找当前文本输入框中显示的文字

    textField.text = @"";

    //文本颜色

    textField.textColor = [UIColor redColor];

    //设置文本字体大小

    textField.font = [UIFont systemFontOfSize:20];

    //设置背景颜色

    textField.backgroundColor = [UIColor lightGrayColor];

    //当重复开始编辑时候 清除文字

    textField.clearsOnBeginEditing = YES;

    //文字提示

    textField.placeholder = @"请输入您的大区名字";

    //文字密文(暗文) 该属性通常用于设置密码输入框

    textField.secureTextEntry = NO;

    //文字输入时的对齐方式

    textField.textAlignment = NSTextAlignmentCenter;

    //文字输入的清除按钮

    /*

     UITextFieldViewModeWhileEditing 当输入时

     UITextFieldViewModeAlways 总是

     UITextFieldViewModeUnlessEditing 不在输入时候

     */

    textField.clearButtonMode = UITextFieldViewModeWhileEditing;

    //键盘的类型

    textField.keyboardType = UIKeyboardTypeDefault;

    //retuan键类型 可自定义键盘

    textField.returnKeyType = UIReturnKeyJoin;

    //左视图

    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];

    label.text = @"账号";

    label.textAlignment = NSTextAlignmentCenter;

    

    textField.leftView = label;

    

    textField.leftViewMode = UITextFieldViewModeWhileEditing;

    

    //右视图

    UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    

    [button setTitle:@"确定" forState:UIControlStateNormal];

    

    button.frame = CGRectMake(0, 0, 50, 50);

    

    [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

    

    textField.rightView = button;

    

    textField.rightViewMode = UITextFieldViewModeAlways;

    

    [self.window addSubview:textField];

    

    //让键盘产生第一响应 键盘会自动弹起

    [textField becomeFirstResponder];

    //收起键盘

    /*

     1、点击键盘的return键

     2、点击Button

     3、点击空白处弹回键盘

     */

  

/*

     手势

     */

    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];

自定制方法/手势方法

- (void)tapClick{

    UITextField * textField = (UITextField*)[self.window viewWithTag:100];

    

    [textField resignFirstResponder];

}

- (void)buttonClick:(UIButton*)button{

    //取消第一响应

    UITextField * textFiled = (UITextField*)[self.window viewWithTag:100];

    

    [textFiled resignFirstResponder];

}

所有代理方法作用

//当Return键被点击时调用 通常用于收回键盘

- (BOOL)textFieldShouldReturn:(UITextField *)textField{

    

    [textField resignFirstResponder];

    

    return YES;//5.1前设置NO为点击无效

}

//文本输入框开始输入时调用

- (void)textFieldDidBeginEditing:(UITextField *)textField{

    //将键盘弹出

    NSLog(@"开始输入");

}

//文本输入框结束输入时调用

- (void)textFieldDidEndEditing:(UITextField *)textField{

    //获取当前文本输入框中所输入的文字

    NSLog(@"所输入的内容为:%@",textField.text);

    //例:判断账号书写形式是否正确 如果不正确提示填写错误 重新输入

    

    NSLog(@"结束输入");

}

//文本输入框内容发生变化即会调用的方法

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    /*

    NSLog(@"内容:%@",textField.text);//获取的是上一次所输入内容

    NSLog(@"Location:%lu Length:%lu",range.location,range.length);//范围为当前文字的位置,长度为零

    

    NSLog(@"==%@==",string);//实时获取当前输入的字符

    */

    //需求 实时获取当前文本框中的所有文字

    NSString * resultStr = [textField.text stringByAppendingString:string];

    

    NSLog(@"%@",resultStr);

    

    //可在该方法中判断所输入文字是否正确

    

    return YES;

}

//了解

//是否允许文本输入框可以输入

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{

    return YES;

}

//是否允许文本输入框结束

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{

    //在该方法中可以通过判断文本长度限制键盘是否可以收回

    return NO;

}

//是否允许被清除

- (BOOL)textFieldShouldClear:(UITextField *)textField{

    NSLog(@"文字被清除");

    return YES;

}

原文地址:https://www.cnblogs.com/sunfuyou/p/5915781.html