///别忘在 .h 中写代理 <UITextViewDelegate>
///UILabel 显示的文本只读,无法编辑,可以根据文字个数自动换行; ///UITextField 可编辑本文,但是无法换行,只能在一行显示;当点击键盘上的return时会收到一个事件做一些事情。 ////UITextView 可编辑文本,提供换行功能。
代码语言:javascript复制UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 230, 300, 180)];
textField.tag = 100;
//更改背景颜色
//textField.backgroundColor = [UIColor greenColor];
//边框类型
textField.borderStyle = UITextBorderStyleRoundedRect;
//字体
textField.font = [UIFont boldSystemFontOfSize:60.0];
//字体颜色
textField.textColor = [UIColor blueColor];
//对齐方式
textField.textAlignment = NSTextAlignmentLeft;
//垂直对齐
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//水平对齐
//textField.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
//文字缩放
textField.adjustsFontSizeToFitWidth = YES;
//缩放后最小字号
textField.minimumFontSize = 40.0;
//文本
//textField.text = @"请输入账号";
//占位文字
textField.placeholder = @"请输入账号";
//清空按钮
textField.clearButtonMode = UITextFieldViewModeAlways;
//当编辑时清空
//textField.clearsOnBeginEditing = YES;
//自动大写
//textField.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
//单词检测 是否是单词 显示下划线
textField.autocorrectionType = UITextAutocorrectionTypeNo;
//textField.background
textField.delegate = self;
//键盘样式
//textField.keyboardAppearance = UIKeyboardAppearanceAlert;
//键盘类型
textField.keyboardType = UIKeyboardTypeEmailAddress;
textField.returnKeyType = UIReturnKeyGo;
//密码
textField.secureTextEntry = YES;
//圆角
textField.layer.cornerRadius = 5.0 //导入QuartzCore.framework, 引用#import <QuartzCore/QuartzCore.h>
//光标过于靠前
textField.borderStyle = UITextBorderStyleRoundedRect;
[self.window addSubview:textField];
/////方法/消息 点击屏幕空白处调用
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITextField* textField = (UITextField*)[self.window viewWithTag:100];
//让键盘下去
[textField resignFirstResponder];
NSUserDefaults* user = [NSUserDefaults standardUserDefaults];
NSString* email = [user objectForKey:@"email"];
}
//delegate
//是否允许开始编辑
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"开始编辑");
//textField.frame = CGRectMake(10, 230 - 200, 300, 180);
self.window.frame = CGRectMake(0, -200, 320, 480);
}
//是否允许结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
//结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField{
self.window.frame = CGRectMake(0, 0, 320, 480);
}
//是否允许改变内容
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
//NSLog(@"%d----%d",range.location,range.length);
NSLog(@"%@",string);
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
//键盘右下角return键
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
NSLog(@"触发");
return YES;
}