Textfield的常用方法

Tips:

如果不弹出键盘

在启动模拟器的时候 菜单顶部有一个hardware按钮,里面有一个keyboard选项,点一下toggle blabla的那个选项,应该就行了

    /*
     *  初始化Textfiled
     *
     */
    
    UITextField *tf = [[UITextField alloc]initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, 40)];
    
    //更改提示信息内容
    tf.placeholder = @"";

    //更改return显示效果
    
    /*
     UIReturnKeyDefault,        -默认效果
     UIReturnKeyGo,             -开始
     UIReturnKeyGoogle,         -搜索
     UIReturnKeyJoin,           -加入
     UIReturnKeyNext,           -下一步
     UIReturnKeySearch,         -搜索
     UIReturnKeySend,           -发送
     UIReturnKeyYahoo,          -日本
     UIReturnKeyDone,           -结束
     UIReturnKeyEmergencyCall,
     */
    tf.returnKeyType = UIReturnKeyDone;

    //键盘外观样式
    
    /*
     UIKeyboardAppearanceDefault,          // Default apperance for the current input method.
     UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),
     UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),
     UIKeyboardAppearanceAlert
     */
    
    tf.keyboardAppearance = UIKeyboardAppearanceAlert;
    
    //键盘类型
    /*
     UIKeyboardTypePhonePad - 电话键盘
     UIKeyboardTypeNumberPad - 纯数字
     UIKeyboardTypeEmailAddress - email地址键盘
     */
    tf.keyboardType = UIKeyboardTypeASCIICapable;
    
    //秘密输入
    tf.secureTextEntry = YES;

    //内容的对齐方式
    tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    tf.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    //清空按键出现的时机
    tf.clearButtonMode = UITextFieldViewModeAlways;
    
    /*
     UITextBorderStyleNone,         -默认
     UITextBorderStyleLine,         -直线框
     UITextBorderStyleRoundedRect   -曲线框
     */
    tf.borderStyle =  UITextBorderStyleLine;
    
    tf.delegate = self;

    //自定义_clearButton(修改clearButton图片样式)

    UIButton *clearButtonpassword = [tf valueForKey:@"clearButton"];
    UIImage *image = [UIImage imageNamed:@"Login_icon_delete"];
    [clearButtonpassword setImage:image forState:UIControlStateNormal];

    
    self.tf = tf;
    
    [self.view addSubview:tf];
    
}


/*
    TextField - delegate
    三个常用方法
 
 textFieldShouldReturn - 取消响应事件
 shouldChangeCharactersInRange - 修改文字
 textFieldShouldEndEditing - 用户输入结束后,需要获得输入的内容
 */

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //点击returen之后的方法
    //取消键盘输入
    //[textField resignFirstResponder];
    
//输入用户名(这里的textField最好创建一个UIView类,便于复用)
     if (textField == _usernametextField)
    {
        [_usernametextField resignFirstResponder];
        [_passwordtextField becomeFirstResponder];
    }
//输入密码
    else if (textField == _passwordtextField)
    {
        [_passwordtextField resignFirstResponder];
        if (!txtAuthCodetextField.hidden)//如果验证码框存在,弹出键盘
            [txtAuthCodetextField becomeFirstResponder];
    }
//输入验证码
    else if (textField == txtAuthCodetextField)
    {
        [txtAuthCodetextField resignFirstResponder];
    }

    return YES;
}

//输入内容发生改变就会触发shouldChangeCharactersInRange
//包括输入,删除,粘贴内容
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSLog(@"replacementString - %@",string);
    return YES;
}


//用户输入结束后,需要获得输入的内容,在这里
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    NSLog(@"textField - text - %@",textField.text);
    return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // 当捕捉到触摸事件时,取消UITextField的第一相应
    [self removeResignFirstResponder];
}

 

转载于:https://my.oschina.net/sgcllr/blog/1329695

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值