文本输入框的学习。
初始化方式:
//文本输入框的学习
UITextField *myTextFied = [[UITextField alloc]initWithFrame:CGRectMake(100, 100, 150, 50)];
//设置占位符
myTextFied.placeholder = @"请输入三围尺寸";
//显示texfied
[self.window addSubview:myTextFied];
设置软键盘样式:
//设置键盘样式
myTextFied.keyboardType = UIKeyboardTypeNumberPad;//纯数字的键盘
设置边框样式:
//设置边框样式
myTextFied.borderStyle = UITextBorderStyleRoundedRect;
设置加密样式:
//设置加密样式
[myTextFied setSecureTextEntry:YES];
//设置右侧小叉号
[myTextFied setClearButtonMode:UITextFieldViewModeAlways];
设置左视图:
//设置左视图
UIImageView *leftView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"110.png"]];
[myTextFied setLeftView:leftView];
[myTextFied setLeftViewMode:UITextFieldViewModeAlways];
同理有设置右视图的方式。
//设置右视图
UIImageView *rightView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"110.png"]];
[myTextFied setRightView:rightView];
[myTextFied setRightViewMode:UITextFieldViewModeUnlessEditing];
//自定义键盘
UIView *keyboardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 256)];
myTextFied.inputView = keyboardView;
//添加一个按钮
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
[btn setTitle:@"1" forState:UIControlStateNormal];
[btn setFrame:CGRectMake(0, 0, 50, 50)];
[keyboardView addSubview:btn];
[btn setBackgroundColor:[UIColor whiteColor]];
[btn addTarget:self action:@selector(textFieldAction:) forControlEvents:UIControlEventTouchUpInside] ;
myTextFied.tag = 2000;
- (void)textFieldAction:(UIButton *)sender{
//点击按钮使按钮的标题显示在输入框中
UITextField *temp = (UITextField*)[self.window viewWithTag:2000];
//得到按钮标题
NSString *tittle = [sender titleForState:UIControlStateNormal];
temp.text = [temp.text stringByAppendingString:tittle];
}
代理方法的学习:
初始化一个textfield。
//输入框代理方法的学习
UITextField *delegateText = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 100, 100)];
delegateText.borderStyle = UITextBorderStyleLine;
delegateText.placeholder = @"代理方法的学习";
设置代理方法:
//设置代理方法
delegateText.delegate = self;
[self.window addSubview:delegateText];
注意在这里为什么要用self去代理,而且在这里self指的是什么得弄清楚。
在写代理方法时,首先在头文件
"AppDelegate.h"
中加入代理协议<UITextFieldDelegate>然后再在"AppDelegate.m"文件中写代理相关的方法。
在这里我们发现,写完代理的相关方法后,就可以是实现对textfield相关操作了。
#pragma mark - 代理方法
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
NSLog(@"%s",__func__);
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"%s",__func__);
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"%s",__func__);
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"%s",__func__);
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"%s",__func__);
[textField resignFirstResponder];
return YES;
}
从上到下依次是是否开始编辑,已经开始编辑,是否结束编辑,和已经结束编辑。相关用法在ui中比较重要。望查阅相关资料进行深入理解。