UITextField
2015-07-28 21:16 南峰_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]; } }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何打造一个高并发系统?
· .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不一致处