iOS UI基础 - 20 UITextField
2016-04-01 00:05
447 查看
//找到已经创建好的UITextField UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, RFSCREEN_W - 100 , 45)]; textField.leftImageName = @"login_user_icon" //设置边框样式 textField.borderStyle = UITextBorderStyleNone; //通过layer设置圆角 //textField.layer.cornerRadius = 10; textField.layer.borderColor = Color_White.CGColor; textField.layer.borderWidth = 1; // 设置字体颜色 textField.textColor = Color_White; // 输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容 textField.clearButtonMode = UITextFieldViewModeAlways; //第一响应者,一个界面可能有多个可输入的控件,哪一个正在编辑哪一个就是第一响应者 [textField becomeFirstResponder]; // 设置左边图标 UIImageView *leftImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"login_user_icon"]]; leftImageView.frame = CGRectMake(0, 0, 50, 50); leftImageView.contentMode = UIViewContentModeCenter; textField.leftView = leftImageView; //设置左视图的显示模式 textField.leftViewMode = UITextFieldViewModeAlways; // 设置键盘的样式 textField.keyboardType = UIKeyboardTypeNumberPad; // 设置占位文字 textField.placeholder = @"请输入用户名"; // 设置占位文字的颜色 [textField setValue:Color_White forKeyPath:@"placeholderLabel.textColor"]; [self.view addSubview:textField];
限定只可以输入数字和6位长度
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { NSString *text = [textField text]; NSCharacterSet *characterSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789\b"]; string = [string stringByReplacingOccurrencesOfString:@" " withString:@""]; if ([string rangeOfCharacterFromSet:[characterSet invertedSet]].location != NSNotFound) { return NO; } text = [text stringByReplacingCharactersInRange:range withString:string]; text = [text stringByReplacingOccurrencesOfString:@" " withString:@""]; if ([text stringByReplacingOccurrencesOfString:@" " withString:@""].length >= 7) { return NO; } [textField setText:text]; _confirmButton.enabled = text.length > 0 ? YES : NO; return NO; }
相关文章推荐
- 简介EasyUI datagrid editor combogrid搜索框的实现
- EasyUi中的Combogrid 实现分页和动态搜索远程数据
- EasyUi combotree 实现动态加载树节点
- 非UI线程使用setVisibility发生的bug
- OC中的UIButton/UIlable
- iOS边练边学--触摸事件以及能够拖拽的UIView的练习
- Fluent NHibernate and Mysql,SQLite,PostgreSQL
- IOS UIScrollView——简单示例
- SPOJ QTREE5 Query on a tree V(边分治)
- SPOJ QTREE4 Query on a tree IV(边分治)
- SPOJ PT07J Query on a tree III(dfs序,主席树)
- SPOJ QTREE2 Query on a tree II(lct)
- SPOJ QTREE Query on a tree (lct)
- webpack初学者指南(Beginner’s guide to Webpack)
- IOS UIScrollView——用法详解
- could not build module foundation 解决
- 在requirejs中使用qunit
- 微软 Build 2016
- SPOJ GSS5 Can you answer these queries V(区间合并)
- UIView的alpha、hidden和opaque属性之间的关系和区别