UITextField

需要加入UITextFieldDelegate协议

- (void)viewDidLoad

{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    UITextField *textTest = [[UITextField alloc] initWithFrame:CGRectMake(50, 50, 100, 30)];
    textTest.text = @"myText";
    textTest.delegate = self;//设置delegate为自己,不然按下Done键键盘消失不了
    //设置框类型
    [textTest setBorderStyle:UITextBorderStyleRoundedRect];
//    textTest.borderStyle = UITextBorderStyleRoundedRect;
    textTest.placeholder = @"输入"; //默认显示的内容
    textTest.secureTextEntry = NO; //是否以密码形式显示
    textTest.returnKeyType = UIReturnKeyDone; //设置键盘返回形式
    textTest.clearButtonMode = UITextFieldViewModeWhileEditing; //输入内容后会显示个X
    textTest.keyboardType = UIKeyboardTypeDefault; //键盘显示类型
    textTest.autocorrectionType = UITextAutocorrectionTypeNo; //是否自动提醒功能
    textTest.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; //设置输入方式
    textTest.autoresizingMask = UIViewAutoresizingFlexibleWidth;  //自适应宽度
    textTest.tag = 10;

    [self.view addSubview:textTest];

}

//开始

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
   
}
//结束
-(void)textFieldDidEndEditing:(UITextField *)textField{
   
}
//按Done键键盘消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    UITextField *text = (UITextField*)[self.view viewWithTag:10];
    [text resignFirstResponder];


    return YES;
}

//按非键盘位置的其它任意位置让键盘消失

//该方法需要先把XIB里的Objects下的UIView类名改为UIControl,然后右键点击Objects下的Control,把鼠标放开事件连线到File`s Owner
- (IBAction)backgroundTap:(id)sender {
    UITextField *text = (UITextField*)[self.view viewWithTag:10];
    [text resignFirstResponder];
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值