代码改变世界

UITextField

  南峰_ldl  阅读(349)  评论(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];
    }
}
复制代码

 

编辑推荐:
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 现代计算机视觉入门之:什么是图片特征编码
· .NET 9 new features-C#13新的锁类型和语义
· Linux系统下SQL Server数据库镜像配置全流程详解
阅读排行:
· Sdcb Chats 技术博客:数据库 ID 选型的曲折之路 - 从 Guid 到自增 ID,再到
· 语音处理 开源项目 EchoSharp
· 《HelloGitHub》第 106 期
· Huawei LiteOS基于Cortex-M4 GD32F4平台移植
· mysql8.0无备份通过idb文件恢复数据过程、idb文件修复和tablespace id不一致处
点击右上角即可分享
微信分享提示