您的位置:首页 > 产品设计 > UI/UE

iOS阶段学习第29天笔记(UITextField的介绍)

2015-07-30 21:01 399 查看
iOS学习(UI)知识点整理

一、关于UITextField的介绍

1)概念: UITextField 是用于接收用户输入的一个控件

2)UITextField 初始化实例代码:

//创建一个UItextField实例
UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(10, 40, self.view.frame.size.width - 20, 40);
textField.backgroundColor = [UIColor lightGrayColor];
//设置textFiled中的文字
textField.text = @"用户名";
//设置textFiled的字体
textField.font = [UIFont systemFontOfSize:20];
//设置textFiled的文字颜色
textField.textColor = [UIColor purpleColor];
[self.view addSubview: textField];


3)textAlignment 设置文本的对齐方式 例如:

//设置textFiled的文本左对齐
textField.textAlignment = NSTextAlignmentLeft;


4)borderStyle 设置文本框样式 例如:

//设置textFiled样式为无边框样式
textField.borderStyle = UITextBorderStyleNone;
//borderStyle 有以下几种类型
//1、UITextBorderStyleNone,
//2、UITextBorderStyleLine,
//3、UITextBorderStyleBezel,
//4、UITextBorderStyleRoundedRect


5)layer.cornerRadius 设置文本框圆角 例如:

textField.layer.cornerRadius = 4.0f;


6)layer.borderWidth 设置文本框边框宽度 例如:

textField.layer.borderWidth = 1;


7)layer.borderColor 设置文本框边框颜色 例如:

textField.layer.borderColor = [UIColor darkGrayColor].CGColor;



8)background 设置文本框背景图片 例如:

UIImage *image = [UIImage imageNamed:@"btnEmojBtn"];
textField.background = image;


9)设置文本框默认显示文字(默认灰色字体点击键盘输入时文字消失)例如:

方法一:placeholder

textField.placeholder = @"用户名";


方法二: NSMutableAttributedString

NSMutableAttributedString *muAttStr = [[NSMutableAttributedString alloc] initWithString:@"用户名"];
[muAttStr addAttribute:NSForegroundColorAttributeName value:
[UIColor blueColor]    range:NSMakeRange(0, muAttStr.length)];
textField.attributedPlaceholder = muAttStr;


10)clearButtonMode 设置文本框的清除内容按钮显示模式(即文本框右边的小叉) 例如:

textField.clearButtonMode = UITextFieldViewModeWhileEditing;

typedef enum {
UITextFieldViewModeNever, //从不出现
UITextFieldViewModeWhileEditing, //编辑时出现
UITextFieldViewModeUnlessEditing,  //除了编辑外都出现
UITextFieldViewModeAlways   //一直出现
} UITextFieldViewMode;


11)leftView 设置文本框的左边视图 例如:

UIView *leftView = [[UIView alloc] init];
leftView.backgroundColor = [UIColor clearColor];
leftView.frame = CGRectMake(0, 0, 50, 40);
textField.leftView = iconImgView;
//设置文本框左边视图的出现方式
textField.leftViewMode = UITextFieldViewModeAlways;


12)returnKeyType 设置文本框呼出的键盘右下角按钮类型 例如:

textField.returnKeyType = UIReturnKeyDone;


13)userInteractionEnabled 设置文本框是否可编辑 例如:

//设置文本框不可编辑
textField.userInteractionEnabled=NO;


14)becomeFirstResponder 设置文本框成为第一响应者即呼出键盘 例如:

[textField becomeFirstResponder];
//注意:当设置一个控件为第一响应者之后,再设置其他为第一响应者无效 第一响应者有且只能有一个


15)resignFirstResponder 设置文本框放弃第一响应者即隐藏键盘 例如:

[textField resignFirstResponder];


16)secureTextEntry 设置文本框密文模式即密码框 例如:

textField .secureTextEntry = YES;


17)keyboardType 设置文本框呼出键盘类型 例如:

//设置数字键盘
textField.keyboardType = UIKeyboardTypeNumberPad;

//keyboardType 有:
typedef enum {
UIKeyboardTypeDefault,      // 默认键盘,支持所有字符
UIKeyboardTypeASCIICapable,  //支持ASCII的默认键盘
UIKeyboardTypeNumbersAndPunctuation,  //标准电话键盘,支持+*#字符
UIKeyboardTypeURL,            //URL键盘,支持.com按钮 只支持URL字符
UIKeyboardTypeNumberPad,   //数字键盘
UIKeyboardTypePhonePad,    // 电话键盘
UIKeyboardTypeNamePhonePad,   //电话键盘,也支持输入人名
UIKeyboardTypeEmailAddress,   //用于输入电子 邮件地址的键盘
UIKeyboardTypeDecimalPad,     //数字键盘 有数字和小数点
UIKeyboardTypeTwitter,        //优化的键盘,方便输入@、#字符
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable,
} UIKeyboardType;


18)adjustsFontSizeToFitWidth 设置文本框内容超出文本框范围时文字自动变化大小 例如:

textField.adjustsFontSizeToFitWidth = YES;
//设置自动缩小显示的最小字体大小
textField.minimumFontSize = 20;


19)keyboardAppearance 设置键盘外观 例如:

//深灰 石墨色键盘
2 textField.keyboardAppearance=UIKeyboardAppearanceAlert;
3 typedef enum {
4 UIKeyboardAppearanceDefault, //默认外观,浅灰色
5 UIKeyboardAppearanceAlert,    // 深灰 石墨色
6 } UIReturnKeyType;


20)文本框 内容对齐方式 例如:

//内容的垂直对齐方式  UITextField继承自UIControl,此类中有一个属性contentVerticalAlignment
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;


21)font 设置文本框字体样式和大小 例如:

textField.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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: