//1.边框
textField.borderStyle =UITextBorderStyleRoundedRect;//凹陷边框,UITextBorderStyleNone 无边框,UITextBorderStyleLine 线边框,UITextBorderStyleRoundedRect圆角边框。
//2.提示语
textField.placeholder = @"请此处输入";
//3.一键删除的那个小叉
textField.clearButtonMode = UITextFieldViewModeAlways;
//4.键盘右下角按钮类型
textField.returnKeyType = UIReturnKeyDone;
//5.键盘的类型
// textField.keyboardType = UIKeyboardTypeDefault;
//6.首字母大写
textField.autocapitalizationType = UITextAutocapitalizationTypeSentences;//UITextAutocapitalizationTypeAllCharacters全是大写,UITextAutocapitalizationTypeSentences句子首字母大写。
//7.自动改正(只适用于英文)
textField.autocorrectionType = UITextAutocorrectionTypeYes;
//8.密文显示(密码输入时候,看不到)
textField.secureTextEntry = YES;
//9.左视图、右视图(x,y无效,宽和高有效)
UIView *leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
leftView.backgroundColor = [UIColor redColor];
textField.leftView = leftView;
textField.leftViewMode = UITextFieldViewModeAlways;
[leftView release];
//10.改变字体
textField.font = [UIFont systemFontOfSize:20];
//11.当字数过多的时候,可以适应宽度。
textField.adjustsFontSizeToFitWidth = YES;
//12.字体最小号
textField.minimumFontSize = 2;
//自定义键盘
UIView *myKeyBoard = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 251)];
myKeyBoard.backgroundColor = [UIColor brownColor];
textField.inputView = myKeyBoard;
textField.tag = 5000;
self.textFieldStr = [NSMutableString stringWithCapacity:0];
for(int i =0;i<10;i++)
{
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setTitle:[NSString stringWithFormat:@"%d",i] forState:UIControlStateNormal];
btn.frame = CGRectMake(i*32, 5,32, 32);
[btn addTarget:self action:@selector(btnDown:) forControlEvents:UIControlEventTouchUpInside];
[myKeyBoard addSubview:btn];
}
设置UITextField 键盘小消失:(在.h文件中包含UITextFieldDelegated协议)
//这个方法点击右下角returnKey时触发。
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"右下角按钮被点击的时候,textfield的delegate属性调用了当前方法,因为已经把我们这个类的对象self赋值给了textField的delegate属性,所以就相当于用我们这个类的对象来调用这个方法了,所以在这里实现它。");
//一般来书哦这个方法里要做的工作有两个,
//第一就是把当前textField的第一响应(正在编辑的状态)取消----导致键盘隐藏。
[textField resignFirstResponder];//取消第一响应
//第二个要做的就是如果有多个输入快的话,切换到下一个输入块。
if(textField.tag!=5001)
{
//找到当前textField的下一个textField
UITextField *nextText = (UITextField *)[self.window viewWithTag:textField.tag+1];
//开始第一响应
[nextText becomeFirstResponder];
//不是最后一个就继续
}
else//==5001
{
self.window.frame = CGRectMake(0, 0, 320, 480);
//是最后一个就屏幕归位。
}
return YES;
}
设置视图上移:
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
//一般只做一件事,就是把被挡住的textfield露出来。
self.window.frame = CGRectMake(0, -216, 320, 480);
//把父视图的frame向上移动。
}
典例:
实现输入几,下面界面就打印几个view。(设置一个静态变量,用来保存上一次获得的值,当有新值的时候,先执行remove操作把上次的view删掉,再打印新的view数量。)
入口函数代码:
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 250, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.window addSubview:textField];
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.placeholder =@"input";
textField.tag = 5000;
textField.delegate = self;
[textField release];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(270, 20, 40, 30);
[btn setTitle:@"go" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnDown) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn];
入口函数代码:
UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 250, 30)];
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.window addSubview:textField];
textField.clearButtonMode = UITextFieldViewModeAlways;
textField.placeholder =@"input";
textField.tag = 5000;
textField.delegate = self;
[textField release];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(270, 20, 40, 30);
[btn setTitle:@"go" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnDown) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:btn];
此处为点击button后响应方法:
-(void)btnDown
{
UITextField *textField = (UITextField *)[self.window viewWithTag:5000];
static int a =0;//重点之处。
for(int i =0;i<a;i++)
{
UIView *tempView = (UIView *)[self.window viewWithTag:7000+i];
[tempView removeFromSuperview];
}
a = [textField.text intValue];
for(int i =0;i<a;i++)
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10+i%4*(5+270/4.0), 60+i/4*(30+5), 270/4.0, 30)];
view.backgroundColor = [UIColor greenColor];
[self.window addSubview:view];
view.tag = 7000+i;
[view release];
}
NSLog(@"%d",a);
}
此处为点击button后响应方法:
-(void)btnDown
{
UITextField *textField = (UITextField *)[self.window viewWithTag:5000];
static int a =0;//重点之处。
for(int i =0;i<a;i++)
{
UIView *tempView = (UIView *)[self.window viewWithTag:7000+i];
[tempView removeFromSuperview];
}
a = [textField.text intValue];
for(int i =0;i<a;i++)
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10+i%4*(5+270/4.0), 60+i/4*(30+5), 270/4.0, 30)];
view.backgroundColor = [UIColor greenColor];
[self.window addSubview:view];
view.tag = 7000+i;
[view release];
}
NSLog(@"%d",a);
}