ios UItextfield 入门
批量创建UITextField 写成方法然后调用
-(UITextField *)creat_textField : (CGRect)frame { UITextField *text_field = [[UITextField alloc]initWithFrame:frame]; //text_field的位置和大小 text_field.backgroundColor = [UIColor whiteColor]; //text_field背景颜色 text_field.borderStyle = UITextBorderStyleRoundedRect; //text_field边框为圆角 text_field.clearsOnBeginEditing = YES; //text_field在开始编辑时清空 text_field.keyboardType = UIKeyboardTypeNumberPad ; //text_field键盘为数字键盘 text_field.delegate = self; //text_field需要实现代理 text_field.layer.borderColor = [[UIColor blackColor]CGColor]; //text_field边框的颜色 text_field.layer.borderWidth = 1.0; //text_field边框的宽度 text_field.layer.cornerRadius = 5.0; //text_field边框圆角的半径 [self.view addSubview:text_field]; //加载text_field return text_field; } -(void)CreatTextField //创建textField { self.textField1 = [self creat_textField:CGRectMake(self.view.frame.size.width/6, self.view.frame.size.height/3, self.view.frame.size.width/7, self.view.frame.size.height/15)]; self.textField2 = [self creat_textField:CGRectMake(self.view.frame.size.width/3, self.view.frame.size.height/3, self.view.frame.size.width/7, self.view.frame.size.height/15)]; self.textField3 = [self creat_textField:CGRectMake(self.view.frame.size.width/2, self.view.frame.size.height/3, self.view.frame.size.width/7, self.view.frame.size.height/15)]; self.textField4 = [self creat_textField:CGRectMake(self.view.frame.size.width/1.5, self.view.frame.size.height/3, self.view.frame.size.width/7, self.view.frame.size.height/15)]; }
//设置背景
text.background = [UIImage imageNamed:@"dd.png"];//UITextField 的背景,注意只有UITextBorderStyleNone的时候改属性有效
//设置背景
text.disabledBackground = [UIImage imageNamed:@"cc.png"];
//当输入框没有内容时,水印提示 提示内容为password
text.placeholder = @"password";
//设置输入框内容的字体样式和大小
text.font = [UIFont fontWithName:@"Arial" size:20.0f];
//设置字体颜色
text.textColor = [UIColor redColor];
//输入框中是否有个叉号,在什么时候显示,用于一次性删除输入框中的内容
text.clearButtonMode = UITextFieldViewModeAlways;
UITextFieldViewModeNever, 重不出现
UITextFieldViewModeWhileEditing, 编辑时出现
UITextFieldViewModeUnlessEditing, 除了编辑外都出现
UITextFieldViewModeAlways 一直出现
//输入框中一开始就有的文字
text.text = @"一开始就在输入框的文字";
//每输入一个字符就变成点 用语密码输入
text.secureTextEntry = YES;
//是否纠错
text.autocorrectionType = UITextAutocorrectionTypeNo;
UITextAutocorrectionTypeDefault, 默认
UITextAutocorrectionTypeNo, 不自动纠错
UITextAutocorrectionTypeYes, 自动纠错
//再次编辑就清空
text.clearsOnBeginEditing = YES;
//内容对齐方式
text.textAlignment = UITextAlignmentLeft;
//内容的垂直对齐方式 UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment
text.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
//设置为YES时文本会自动缩小以适应文本窗口大小.默认是保持原来大小,而让长文本滚动
textFied.adjustsFontSizeToFitWidth = YES;
//设置自动缩小显示的最小字体大小
text.minimumFontSize = 20;
//设置键盘的样式
text.keyboardType = UIKeyboardTypeNumberPad;
UIKeyboardTypeDefault, 默认键盘,支持所有字符
UIKeyboardTypeASCIICapable, 支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation, 标准电话键盘,支持+*#字符
UIKeyboardTypeURL, URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad, 数字键盘
UIKeyboardTypePhonePad, 电话键盘
UIKeyboardTypeNamePhonePad, 电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress, 用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad, 数字键盘 有数字和小数点
UIKeyboardTypeTwitter, 优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
//首字母是否大写
text.autocapitalizationType = UITextAutocapitalizationTypeNone;
UITextAutocapitalizationTypeNone, 不自动大写
UITextAutocapitalizationTypeWords, 单词首字母大写
UITextAutocapitalizationTypeSentences, 句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, 所有字母都大写
//return键变成什么键
text.returnKeyType =UIReturnKeyDone;
UIReturnKeyDefault, 默认 灰色按钮,标有Return
UIReturnKeyGo, 标有Go的蓝色按钮
UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin,标有Join的蓝色按钮
UIReturnKeyNext,标有Next的蓝色按钮
UIReturnKeyRoute,标有Route的蓝色按钮
UIReturnKeySearch,标有Search的蓝色按钮
UIReturnKeySend,标有Send的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮
//键盘外观
textView.keyboardAppearance=UIKeyboardAppearanceDefault;
UIKeyboardAppearanceDefault, 默认外观,浅灰色
UIKeyboardAppearanceAlert, 深灰 石墨色
//设置代理 用于实现协议
text.delegate = self;
//把textfield加到视图中
[self.window addSubview:text];
//最右侧加图片是以下代码 左侧类似
UIImageView *image=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"right.png"]];
text.rightView=image;
text.rightViewMode = UITextFieldViewModeAlways;
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
//按return键键盘往下收 becomeFirstResponder
通知
UITextField派生自UIControl,所以UIControl类中的通知系统在文本字段中也可以使用。除了UIControl类的标准事件,你还可以使用下列UITextField类特有的事件
UITextFieldTextDidBeginEditingNotification
UITextFieldTextDidChangeNotification
UITextFieldTextDidEndEditingNotification
当文本字段退出编辑模式时触发。通知的object属性存储了最终文本。
因为文本字段要使用键盘输入文字,所以下面这些事件发生时,也会发送动作通知
UIKeyboardWillShowNotification //键盘显示之前发送
UIKeyboardDidShowNotification //键盘显示之后发送
UIKeyboardWillHideNotification //键盘隐藏之前发送
UIKeyboardDidHideNotification //键盘隐藏之后发送
只限制输入特定长度的字符时,可利用光标的位置来判断
// //-(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString:(NSString *)string //{ // // if (range.location >= 1) // return NO; // return YES; // // //限制输入长度为1 //}
限制输入特定的字符
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString:(NSString *)string{ NSCharacterSet *cs; cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS]invertedSet]; NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串 BOOL canChange = [string isEqualToString:filtered]; return canChange; }
上面那个NUMBERS是一个宏,可以在文件顶部定义:
#define NUMBERS @”0123456789\n”
要求四个textfield输入不同的数字时,利用一个数组把数字放进去,把输入过的数字从数组里去除就可以实现了
-(void)begin_array { m_fieldArray = [[NSArray alloc]initWithObjects:self.textField1.text,self.textField2.text,self.textField3.text,self.textField4.text, nil]; m_beginArray = [[NSMutableArray alloc]initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9", nil]; NSMutableArray *restArray = [[NSMutableArray alloc]initWithCapacity:0]; for (int m= 0; m < m_fieldArray.count; m ++) { for (int n = 0; n < m_beginArray.count ;n++) { if ([[m_fieldArray objectAtIndex:m ] isEqualToString:[m_beginArray objectAtIndex:n]]) { [restArray addObject:[m_beginArray objectAtIndex:n]]; } } } [m_beginArray removeObjectsInArray:restArray]; } -(BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange: (NSRange)range replacementString:(NSString *)string { [self begin_array]; NSString *numbers = [m_beginArray componentsJoinedByString:@""]; NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:numbers]invertedSet]; m_filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; BOOL canChange = [string isEqualToString:m_filtered]; return canChange; }//限制输入的字符 一开始时0-9 比如输入1 限制输入的就会变为0,2-9
限制特殊字符且要限制输入长度时就不能用光标的位置判断了
- (void)textFieldDidBeginEditing:(UITextField *)textField; { [textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged]; } //在开始编辑textField时发送通知 -(void)textFieldDidChange:(UITextField *)textField { if ([ self.textField1 isEqual: textField]||[ self.textField2 isEqual: textField]||[ self.textField3 isEqual: textField]||[ self.textField4 isEqual: textField]) { if (textField.text.length > 1) { textField.text = [textField.text substringToIndex:1]; } }//限制输入的字数为1 }
- ios新手入门之UITextField左边空格
- IOS入门学习笔记(普通UI控件--UISwitch、UITextField、UITextView、UISegmentedControl)
- iOS新手入门之UITextField
- iOS 使用 Core Plot 绘制统计图表入门
- iOS 6 Passbook 入门 2/2
- 手把手教你ARC——iOS/Mac开发ARC入门和使用
- IOS入门学习随笔
- IOS——UITextField自动适应键盘弹出
- IOS 6 自动布局 入门 2
- iOS开发入门实例
- IOS应用开发入门-(IOS项目包含的文件)
- IOS--常用控件--UITextField
- iOS蓝牙开发入门04--服务和特征
- iOS入门(三)循环结构
- iOS开发>>>UITextField输入框,点击背景任何地方,实现键盘关闭
- IOS中UITextField和UILabel简单使用
- iOS入门(二十二)属性
- iOS入门(三十二)UINavigationController
- iOS 入门学习第一课helloworld
- Objective-C编程语言iOS开发入门宝典 - 51CTO.com