UITextField继承:UIContrl:UIView
补充
设置为无文字就灰色不可点
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 30)]
textField.returnKeyType = UIReturnKeySearch; //设置按键类型
textField.enablesReturnKeyAutomatically = YES; //这里设置为无文字就灰色不可点
获取cell中的textField
- (void)textFieldDidEndEditing:(UITextField *)textField{
ZBAddMemberOfFamilyTableViewCell *cell = (ZBAddMemberOfFamilyTableViewCell *)[[textField superview] superview];
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
NSLog(@"%@", cell.rightTextField.text);
}
1.创建UITextField对象
UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(100, 100, 200, 60)];
2.添加到界面上
[self.window addSubview:textField];
3.设置背景颜色
textField.backgroundColor = [UIColor yellowColor];
文字相关的属性
- 1.text(可以属性改变textField上显示文字,更多的时候是通过这个属性去拿到textField的text值)
textField.text = @"hello world";
- 2.文字颜色
textField.textColor = [UIColor redColor];
- 3.设置字体
textField.font = [UIFont systemFontOfSize:20];
- 4.居中模式
textField.textAlignment = NSTextAlignmentLeft;
- 5.占位文字(它的颜色是浅灰色,不能被改变)
textField.text = nil;
显示的条件是当前的text为空
textField.placeholder = @"请输入名字";
- 6.设置占位符颜色 用KVC
textField.placeholder = @"多个标签用逗号或者换行隔开";
// 设置了占位文字内容以后, 才能设置占位文字的颜色
[textField setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
- 7.是否在开始编辑的时候清空文字(默认是 NO)
textField.clearsOnBeginEditing = YES;
- 8.密文显示
textField.secureTextEntry = YES;
显示相关的属性
- 1.设置边框风格
UITextBorderStyleNone, (默认,没有边框)
UITextBorderStyleLine,
UITextBorderStyleBezel,
UITextBorderStyleRoundedRect
[textField setBorderStyle:UITextBorderStyleRoundedRect];
- 2.设置清除按钮模式
UITextFieldViewModeNever, //一直都不显示(默认)
UITextFieldViewModeWhileEditing, //处于编辑状态的时候才显示
UITextFieldViewModeUnlessEditing, //处于非编辑状态的时候显示
UITextFieldViewModeAlways //总是显示
[textField setClearButtonMode:UITextFieldViewModeAlways];
- 3.设置左视图(可以传任意继承自UIView的视图对象)
//设置左视图的坐标没有意义
UILabel * leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 40, 40)];
leftLabel.text = @"账号:";
textField.leftView = leftLabel;
- 4.设置左视图模式(默认是一直不显示)
UITextFieldViewModeNever, //一直都不显示(默认)
UITextFieldViewModeWhileEditing, //处于编辑状态的时候才显示
UITextFieldViewModeUnlessEditing, //处于非编辑状态的时候显示
UITextFieldViewModeAlways //总是显示
[textField setLeftViewMode:UITextFieldViewModeAlways];
- 5.设置自定制键盘
//创建一个视图(只有设置高度是有效的)
UIView * inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 300)];
inputView.backgroundColor = [UIColor yellowColor];
[textField setInputView:inputView];
- 6.设置二级键盘
UIView * accessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 40)];
accessoryView.backgroundColor = [UIColor redColor];
[textField setInputAccessoryView:accessoryView];
编辑相关
- 1.判断当前textFeild是否处于编辑状态
编辑状态:有光标在闪
BOOL ret = textField.isEditing;
NSLog(@"是否处于编辑状态:%@", ret ? @"是": @"不是");
UITextField协议方法
- 1.设置协议
@interface AppDelegate ()<UITextFieldDelegate>
- 2.设置代理
把点前对象作为field的代理,那么必须self的类去遵守协议实现协议方法
field.delegate = self;
- 3.显示清除按钮
[field setClearButtonMode:UITextFieldViewModeAlways];
UITextField Delegate协议内容
- 当用户点击textField弹出的系统键盘上返回键的时候会调用这个方法
- 返回值:是否返回
- 参数:委托
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
//textField放弃第一响应者 (收起键盘)
//键盘是textField的第一响应者
[textField resignFirstResponder];
return YES;
}
- 当文本输入框已经开始编辑的时候会调用这个方法
- (void)textFieldDidBeginEditing:(UITextField *)textField{
}
- 当文本输入框已经停止编辑的时候会调用这个方法
两种停止编辑的情况:1.放弃第一响应者 2.点击了其他的输入框
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"已经停止编辑");
}
- 获取点击键盘按钮的值
返回值:是否通过点击的键盘的按钮的值,去改变textField的text值
参数1:委托
参数2:当前输入字符的位置
参数3:当前输入的字符(以字符串形式返回)
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
NSLog(@"%@", NSStringFromRange(range));
NSLog(@"%@", string);
//按'a'收起键盘
if ([string isEqualToString:@"a"]) {
[textField resignFirstResponder];
}
return YES;
}
- 设置输入框是否可以开始编辑(默认是YES)
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
- 设置输入框是否可以停止编辑 (默认是YES)
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
- 设置清除按钮是否可用
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
添加点击事件
//只要值发生改变就会调用
[_textField addTarget:self action:@selector(textFiledDidChange:) forControlEvents:UIControlEventEditingChanged];
UIControlEventValueChanged = 1 << 12, // sliders, etc.
UIControlEventPrimaryActionTriggered NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 13, // semantic action: for buttons, etc.
UIControlEventEditingDidBegin = 1 << 16, // UITextField
UIControlEventEditingChanged = 1 << 17,
UIControlEventEditingDidEnd = 1 << 18,
UIControlEventEditingDidEndOnExit = 1 << 19, // 'return key' ending editing
UIControlEventAllTouchEvents = 0x00000FFF, // for touch events
UIControlEventAllEditingEvents = 0x000F0000, // for UITextField
UIControlEventApplicationReserved = 0x0F000000, // range available for application use
UIControlEventSystemReserved = 0xF0000000, // range reserved for internal framework use
UIControlEventAllEvents
限制文字输入
http://blog.csdn.net/sinat_25027073/article/details/51151327
[_textField addTarget:self action:@selector(textFiledDidChange:) forControlEvents:UIControlEventEditingChanged];
- (void)textFiledDidChange:(UITextField *)textField{
NSLog(@"%@", textField.text); int length = [self convertToInt:textField.text];
NSLog(@"%d", length);
//如果输入框中的文字大于10,就截取前10个作为输入框的文字 if (length > 10) {
textField.text = [textField.text substringToIndex:5];
}}
//下面这个方法主要是为了判断textField中汉字的个数,一个汉字等于两个字符的长度:
- (int)convertToInt:(NSString *)strtemp
//判断中英混合的的字符串长度{ int strlength = 0; for (int i=0; i< [strtemp length]; i++) {
int a = [strtemp characterAtIndex:i];
if( a > 0x4e00 && a < 0x9fff) {
//判断是否为中文 strlength += 2;
}
} return strlength;
}
设置消息中心,监听值的改变
//监听textFiel的值时时改变
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(textFieldDidChangeValue:)
name:UITextFieldTextDidChangeNotification
object:self.bodyValueTextField];
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
键盘不遮挡视图
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
- (void)keyboardWillChangeFrame:(NSNotification *)note
{
// 键盘显示\隐藏完毕的frame
CGRect frame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 修改底部约束
self.bottomSapce.constant = XMGScreenH - frame.origin.y;
// 动画时间
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 动画 及时刷新
[UIView animateWithDuration:duration animations:^{
[self.view layoutIfNeeded];
}];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}