@implementation HomeViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
UITextField *textField = [[UITextFieldalloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2-150,20,300, 200)];
textField.backgroundColor = [UIColorcyanColor];
textField.tag =1;
//提示输入内容
textField.placeholder = @"用来提示用户";
//根据宽度改变字体大小
textField.adjustsFontSizeToFitWidth =YES;
//设置自动缩小显示的最小字体大小
textField.minimumFontSize = 16;
//设置清除按钮模式
textField.clearButtonMode =UITextFieldViewModeWhileEditing;
/* UITextFieldViewModeNever, 重不出现
UITextFieldViewModeWhileEditing, 编辑时出现
UITextFieldViewModeUnlessEditing, 除了编辑外都出现
UITextFieldViewModeAlways, 一直出现 */
//设置边框类型
textField.borderStyle =UITextBorderStyleRoundedRect;
/* UITextBorderStyleNone,无边框
UITextBorderStyleLine,有边框
UITextBorderStyleBezel,有边框和阴影
UITextBorderStyleRoundedRect圆角 */
//禁止首字母大写
textField.autocapitalizationType =UITextAutocapitalizationTypeNone;
/* UITextAutocapitalizationTypeNone, 不自动大写 UITextAutocapitalizationTypeWords, 单词首字母大写 UITextAutocapitalizationTypeSentences, 句子的首字母大写 UITextAutocapitalizationTypeAllCharacters, 所有字母都大写*/
//设置键盘类型
textField.keyboardType =UIKeyboardTypeNamePhonePad;
/* UIKeyboardTypeDefault, 默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
UIKeyboardTypeURL, URL键盘,支持.com按钮只支持URL字符
UIKeyboardTypeNumberPad, 数字键盘
UIKeyboardTypePhonePad, 电话键盘
UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点
UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,*/
//设置return键类型
textField.returnKeyType = UIReturnKeyDone;
/* UIReturnKeyDefault, 默认 灰色按钮,标有Return UIReturnKeyGo, 标有Go的蓝色按钮 UIReturnKeyGoogle, 标有Google的蓝色按钮,用语搜索 UIReturnKeyJoin, 标有Join的蓝色按钮
UIReturnKeyNext, 标有Next的蓝色按钮
UIReturnKeyRoute, 标有Route的蓝色按钮
UIReturnKeySearch, 标有Search的蓝色按钮
UIReturnKeySend, 标有Send的蓝色按钮
UIReturnKeyYahoo, 标有Yahoo的蓝色按钮
UIReturnKeyYahoo, 标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮*/
//键盘外观
textField.keyboardAppearance =UIKeyboardAppearanceDefault;
/* UIKeyboardAppearanceDefault,默认外观,浅灰色
UIKeyboardAppearanceDark
UIKeyboardAppearanceLight
UIKeyboardAppearanceAlert, 深灰石墨色*/
//是否安全输入,如果是的,输入内容将为“点”
textField.secureTextEntry = YES;
//输入框的字体大小
textField.font = [UIFontboldSystemFontOfSize:48];
//是否纠错
textField.autocorrectionType =UITextAutocorrectionTypeDefault;
/* UITextAutocorrectionTypeDefault, 默认
UITextAutocorrectionTypeNo, 不自动纠错
UITextAutocorrectionTypeYes, 自动纠错 */
//再次编辑就清空
textField.clearsOnBeginEditing =YES;
//内容的水平对齐方式
textField.textAlignment =NSTextAlignmentLeft;
textField.contentHorizontalAlignment =UIControlContentHorizontalAlignmentCenter;
//内容的垂直对齐方式
textField.contentVerticalAlignment =UIControlContentVerticalAlignmentTop;
// 设置弹出视图(不弹键盘弹图片)
UIImageView * imageView = [[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"tree2"]];
imageView.frame = CGRectMake(0, 100, 320, 330);//(跟位置和宽无关)
textField.inputView = imageView;
//最右侧加图片 右视图模式会影响清除按钮模式
UIImageView *image=[[UIImageViewalloc]initWithImage:[UIImageimageNamed:@"money2"]];
textField.rightView=image;
textField.rightViewMode =UITextFieldViewModeAlways;
// 设置左视图
UIView * leftview = [[UIViewalloc]initWithFrame:CGRectMake(0,0,100, 100 )];//只有宽度起到了作用
textField.leftView = leftview;
leftview.backgroundColor = [UIColorpurpleColor];
// 要设置左视图模式
textField.leftViewMode =UITextFieldViewModeAlways;
//设置代理
textField.delegate = self;
[self.viewaddSubview:textField];
//成为第一响应者
[textField becomeFirstResponder];
UIButton *btn = [[UIButtonalloc]initWithFrame:CGRectMake(self.view.bounds.size.width/2-50,250,100, 40)];
[btn setTitle:@"保存"forState:UIControlStateNormal];
btn.backgroundColor = [UIColorpurpleColor];
[btn addTarget:selfaction:@selector(test)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:btn];
}
- (void)test{
UITextField *tf = (UITextField *)[self.viewviewWithTag:1];
[tf resignFirstResponder]; //移除键盘
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textFiel
{
NSLog(@"textFieldShouldBeginEditing");
returnYES; //如果NO,就失去第一响应了
}
//已经开始调用
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textFieldDidBeginEditing %@",textField);
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
NSLog(@"textFieldShouldEndEditing %@",textField);
return YES;
}
//调用结束
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
}
/* 当输入任何字符时,代理调用该方法,如果返回YES则这次输入可以成功,如果返回NO,不能输入成功
range表示光标位置,只有location,length == 0;
string表示这次输入的字符串。 */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *str;
NSLog(@"range = %@ string = %@",NSStringFromRange(range),string);
return str.length <10;
//textField.text超过了10个字符,返回NO,不让输入成功。(最多输入10个)
//textField.text输入后不到10个字符,返回YES,使输入成功。
return YES;
}
- (BOOL)textFieldShouldClear:(UITextField *)textField{
returnYES;//表示可以删除,NO,表示不可以删除
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder]; //失去第一响应
return YES;
}
把textfield或者textview中长按出现的(全选,复制,粘贴) 系统相册等提示语 显示成中文
在info.plist中添加Localized resources can be mixed,设置为YES.
在plist写入这句就是获取当前手机的语言
将“Done”改为中文“完成”:
1、修改语言选择:中文
Localization native development region ,设置为:China
2、修改框架IQKeyboardManager
IQUIView+IQKeyboardToolbar.m文件里面,找到这一个,替换成initWIthTitle这个方法
// IQBarButtonItem *doneButton =[[IQBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:target action:doneAction];
IQBarButtonItem *doneButton =[[IQBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:target action:doneAction];