ios textField添加左右button

在实际开发中,常常需求我们在输入框textField中添加各种辅助视图,例如图:


ios textField添加左右button_第1张图片
示意图.png

实现代码如下:

-(void)configUI{
    self.title = @"修改手机号";
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //手机Icon
    UIButton *telImv = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 30, 20)];
    [telImv setImage:[UIImage imageNamed:@"icon_phone"] forState:UIControlStateNormal];
    telImv.userInteractionEnabled = NO;
    //设置textField 的 rightView属性和 rightViewMode的属性
    self.telTF.leftView = telImv;
    self.telTF.leftViewMode = UITextFieldViewModeAlways;
    //验证码Icon
    UIButton *verifyImv = [[UIButton alloc]initWithFrame:CGRectMake(10, 0, 30, 20)];
    [verifyImv setImage:[UIImage imageNamed:@"icon_code"] forState:UIControlStateNormal];
    verifyImv.userInteractionEnabled = NO;
    self.verifyTF.leftView = verifyImv;
    self.verifyTF.leftViewMode = UITextFieldViewModeAlways;
    //验证码Button
    UIButton *verifyBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    [verifyBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
    [verifyBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
    [verifyBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.verifyTF.rightView = verifyBtn;
    self.verifyTF.rightViewMode = UITextFieldViewModeAlways;
    [verifyBtn addTarget:self action:@selector(getVerifyCode:) forControlEvents:UIControlEventTouchUpInside];
}

判断文字输入状态,正在输入有灰条覆盖

if (!self.textField.markedTextRange)  {
    NSLog(@"输入完成才会进入");
}

验证码倒计时方法,请参考链接:http://www.jianshu.com/p/e806cee61a7b

你可能感兴趣的:(ios textField添加左右button)