一、关于UITextField的介绍
iOS学习(UI)知识点整理
一、关于UITextField的介绍 1)概念: UITextField 是用于接收用户输入的一个控件 2)UITextField 初始化实例代码:1//创建一个UItextField实例2UITextField *textField =[[UITextField alloc] init];3textField.frame = CGRectMake(10,40, self.view.frame.size.width -20,40);4textField.backgroundColor =[UIColor lightGrayColor];5//设置textFiled中的文字6textField.text = @"用户名";7//设置textFiled的字体8textField.font = [UIFont systemFontOfSize:20];9//设置textFiled的文字颜色10textField.textColor =[UIColor purpleColor];11[self.view addSubview: textField];
3)textAlignment 设置文本的对齐方式 例如: 1//设置textFiled的文本左对齐2textField.textAlignment = NSTextAlignmentLeft;
4)borderStyle 设置文本框样式 例如: 1//设置textFiled样式为无边框样式2textField.borderStyle =UITextBorderStyleNone;3//borderStyle 有以下几种类型4//1、UITextBorderStyleNone,5//2、UITextBorderStyleLine,6//3、UITextBorderStyleBezel,7//4、UITextBorderStyleRoundedRect
5)layer.cornerRadius 设置文本框圆角 例如: 1textField.layer.cornerRadius =4.0f;
6)layer.borderWidth 设置文本框边框宽度 例如: 1textField.layer.borderWidth =1;
7)layer.borderColor 设置文本框边框颜色 例如:1textField.layer.borderColor = [UIColor darkGrayColor].CGColor;
8)background 设置文本框背景图片 例如: 1UIImage *image = [UIImage imageNamed:@"btnEmojBtn"];2textField.background = image;
9)设置文本框默认显示文字(默认灰色字体点击键盘输入时文字消失)例如: 方法一:placeholder
1textField.placeholder =@"用户名";
方法二: NSMutableAttributedString
1NSMutableAttributedString *muAttStr = [[NSMutableAttributedString alloc] initWithString:@"用户名"];2[muAttStr addAttribute:NSForegroundColorAttributeName value:3[UIColor blueColor] range:NSMakeRange(0, muAttStr.length)];4textField.attributedPlaceholder = muAttStr;
10)clearButtonMode 设置文本框的清除内容按钮显示模式(即文本框右边的小叉) 例如: textField.clearButtonMode =UITextFieldViewModeWhileEditing;
typedefenum{
UITextFieldViewModeNever,//从不出现UITextFieldViewModeWhileEditing,//编辑时出现UITextFieldViewModeUnlessEditing,//除了编辑外都出现UITextFieldViewModeAlways//一直出现} UITextFieldViewMode;
11)leftView 设置文本框的左边视图 例如:1UIView *leftView =[[UIView alloc] init];2leftView.backgroundColor =[UIColor clearColor];3leftView.frame = CGRectMake(0,0,50,40);4textField.leftView =iconImgView;5//设置文本框左边视图的出现方式6textField.leftViewMode = UITextFieldViewModeAlways;
12)returnKeyType 设置文本框呼出的键盘右下角按钮类型 例如:1textField.returnKeyType = UIReturnKeyDone;
13)userInteractionEnabled 设置文本框是否可编辑 例如: 1//设置文本框不可编辑2textField.userInteractionEnabled=NO;
14)becomeFirstResponder 设置文本框成为第一响应者即呼出键盘 例如: 1[textField becomeFirstResponder];2//注意:当设置一个控件为第一响应者之后,再设置其他为第一响应者无效 第一响应者有且只能有一个
15)resignFirstResponder 设置文本框放弃第一响应者即隐藏键盘 例如:1[textField resignFirstResponder];
16)secureTextEntry 设置文本框密文模式即密码框 例如: 1textField .secureTextEntry = YES;
17)keyboardType 设置文本框呼出键盘类型 例如: 1//设置数字键盘2textField.keyboardType =UIKeyboardTypeNumberPad;34//keyboardType 有:5typedefenum{6UIKeyboardTypeDefault,//默认键盘,支持所有字符7UIKeyboardTypeASCIICapable,//支持ASCII的默认键盘8UIKeyboardTypeNumbersAndPunctuation,//标准电话键盘,支持+*#字符9UIKeyboardTypeURL,//URL键盘,支持.com按钮 只支持URL字符10UIKeyboardTypeNumberPad,//数字键盘11UIKeyboardTypePhonePad,//电话键盘12UIKeyboardTypeNamePhonePad,//电话键盘,也支持输入人名13UIKeyboardTypeEmailAddress,//用于输入电子 邮件地址的键盘14UIKeyboardTypeDecimalPad,//数字键盘 有数字和小数点15UIKeyboardTypeTwitter,//优化的键盘,方便输入@、#字符16UIKeyboardTypeAlphabet =UIKeyboardTypeASCIICapable,17} UIKeyboardType;
18)adjustsFontSizeToFitWidth 设置文本框内容超出文本框范围时文字自动变化大小 例如:1textField.adjustsFontSizeToFitWidth =YES;2//设置自动缩小显示的最小字体大小3textField.minimumFontSize =20;
19)keyboardAppearance 设置键盘外观 例如: //深灰 石墨色键盘2textField.keyboardAppearance=UIKeyboardAppearanceAlert;3typedefenum{4UIKeyboardAppearanceDefault,//默认外观,浅灰色5UIKeyboardAppearanceAlert,//深灰 石墨色6} UIReturnKeyType;
20)文本框 内容对齐方式 例如: 1//内容的垂直对齐方式 UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment2textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
21)font 设置文本框字体样式和大小 例如: 1textField.font = [UIFont fontWithName:@"Arial"size:20.0f];
22)delegate 设置文本框代理对象 例如:textField.delegate=self;//文本框设置代理时当前类先必须遵守 UITextFieldDelegate 协议然后实现它的协议方法//它的协议方法有://返回一个BOOL值,是否允许文本框开始编辑1、- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;//开始编辑时触发,文本框将成为第一响应者(first responder )2、- (void)textFieldDidBeginEditing:(UITextField *)textField;//返回BOOL值,是否允许文本框结束编辑,当编辑结束,当前文本框会放弃第一响应者身份3、- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;//当文本框结束编辑时触发此方法4、- (void)textFieldDidEndEditing:(UITextField *)textField;//当文本框内容改变时触发此方法,可在此方法中对文本内容做非法字符筛选或限制输入5、- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string;//当点击文本框右边清除按钮时触发此方法6、- (BOOL)textFieldShouldClear:(UITextField *)textField;//当点击键盘右下角按钮Return 按钮时触发此方法7、- (BOOL)textFieldShouldReturn:(UITextField *)textField;