UITextField

//设置边框模式
tf.borderStyle = UITextBorderStyleRoundedRect;
//设置最小字体
tf.minimumFontSize = 15;
//设置默认文本
tf.text = @”username”;
//编辑时自动清空
tf.clearsOnBeginEditing = YES;
//设置删除按钮显示模式
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
//开启密码输入模式
//tf.secureTextEntry = YES;
//垂直方向的对齐方式,容易忽略
tf.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//水平方向的对齐方式
tf.textAlignment = NSTextAlignmentCenter;
//设置自动大写
tf.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
//自动校正模式(提示)
tf.autocorrectionType = UITextAutocorrectionTypeYes;
//设置键盘种类
//tf.keyboardType = UIKeyboardTypeNumberPad;
//设置代理
tf.delegate = self;

/设置正常状态下的背景图
tf.background = [UIImage imageNamed:@”normalbg”];
//设置禁用状态下的背景图,在tf.enabled为NO时显示
tf.disabledBackground = [UIImage imageNamed:@”disable”];
//设置左边视图||//设置右边视图,会覆盖掉clearButton
tf.leftView = leftView;
tf.leftViewMode = UITextFieldViewModeAlways;

//代理方法之一
//是否结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField 
{
    if (textField.text.length < 6) {
        return NO;
    }
    return YES;
}
//递归查找第一响应者,自己实现
- (UITextField *)searchFirstResponderOnView:(UIView *)view
{
    for (UITextField *subView in view.subviews) {
        if ([subView isFirstResponder]) {
            return subView;
        } else {
            UITextField *grandchild = [self searchFirstResponderOnView:subView];
            if (grandchild) {
                return grandchild;
            }
        }
    }
    return nil;
}

//定制二级键盘
_textField.inputAccessoryView = [self customAccessoryView];
//设置主键盘
_textField.inputView = view;

#pragma mark - 监听键盘
-(void)monitorKeyboard
{
    //监听键盘弹出
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    //监听键盘收起
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)news
{
    //NSLog(@"%@", news.userInfo);
    NSValue *value = [news.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    //转换成frame
    CGRect frame = [value CGRectValue];

    CGPoint center = _textField.center;
    center.y -= frame.size.height;
    _textField.center = center;
}
- (void)keyboardWillHide:(NSNotification *)news
{
    //NSLog(@"%@", news.userInfo);
    NSValue *value = [news.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    //转换成frame
    CGRect frame = [value CGRectValue];

    CGPoint center = _textField.center;
    center.y += frame.size.height;
    _textField.center = center;
}

//结束监听
[[NSNotificationCenter defaultCenter] removeObserver:self];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值