码迷,mamicode.com
首页 > 其他好文 > 详细

UITextField

时间:2015-07-28 22:52:49      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:

    UITextField是开发当中常用的控件,通常用于外部数据输入,以实现人机交互。下面我对它的一些常用功能做了一个简单的总结。

 

1.更改placeholder的属性

[textField setValue[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue[UIFont boldSystemFontOfSize16] forKeyPath:@"_placeholderLabel.font"];

 

2.点击输入框弹出键盘,并实现了回收键盘的功能,这里使用了UITextField的代理

#pragma mark - UITextFieldDelegate- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [textField resignFirstResponder];
    return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    CGRect frame = _viewTF.frame;
    if (_flag == 20) {
        _flag = frame.origin.y;
    }
    frame.origin.y = 20;
    _viewTF.frame = frame;
    
    return YES;
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    CGRect frame = _viewTF.frame;
    frame.origin.y = _flag;
    _viewTF.frame = frame;
    
    return YES;
}

这样干点击最后一个输入框,再点击上一个输入框,键盘就回去了,用户体验不好,我们可以使用一个通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dealHide:) name:UIKeyboardWillHideNotification object:nil];
-(void)dealShow:(NSNotification *)n
{
    double time = [n.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
    CGRect frame;
    [n.userInfo[@"UIKeyboardFrameEndUserInfoKey"] getValue:&frame];
    CGSize size = [UIScreen mainScreen].bounds.size;
    [UIView animateWithDuration:time animations:^{
        _bottomButton.frame = CGRectMake(0, size.height-frame.size.height - 44, _bottomButton.frame.size.width, _bottomButton.frame.size.height);
    }];
    
    CGRect frameTF = _viewTF.frame;
    if (_flag == 20) {
        _flag = frame.origin.y;
    }
    frameTF.origin.y = 20;
    _viewTF.frame = frameTF;
}
-(void)dealHide:(NSNotification *)n
{
    double time = [n.userInfo[@"UIKeyboardAnimationDurationUserInfoKey"] doubleValue];
    CGRect frame;
    [n.userInfo[@"UIKeyboardFrameEndUserInfoKey"] getValue:&frame];
    CGSize size = [UIScreen mainScreen].bounds.size;
    [UIView animateWithDuration:time animations:^{
        _bottomButton.frame = CGRectMake(0, size.height - 44, _bottomButton.frame.size.width, _bottomButton.frame.size.height);
    }];
    
    CGRect frameTF = _viewTF.frame;
    frameTF.origin.y = _flag;
    _viewTF.frame = frameTF;
}

 

3.输入密码时限制只输入数字,将Secure Text Entry勾选上就可以使用系统的安全输入

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSCharacterSet *cs;
    if(textField == _numberTextField)
    {
        cs = [[NSCharacterSet characterSetWithCharactersInString:@"0123456789\n"] invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
        BOOL basicTest = [string isEqualToString:filtered];
        if(!basicTest)
        {
            return NO;
        }
    }
    return YES;
}

 

4.限制输入字符个数

-(void)create
{
    _codeTextField.delegate = self;
    [_codeTextField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    [_codeTextField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    _win = [[UIApplication sharedApplication].windows objectAtIndex:1];
}
-(void)textFieldDidChange:(UITextField *)textField
{
    if (textField.text.length>4) {
        textField.text = [textField.text substringToIndex:4];
    }
}

 

UITextField

标签:

原文地址:http://www.cnblogs.com/NFli/p/4684372.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!