基本属性:
- 文字属性包括:
<1>.text: 文本内容.(attributedText: 可变的)
<2>.placeholder: 占位文本.(70% gray)(attributedPlaceholder: 可变的)
<3>.font: 字体.
<4>.textColor: 文本颜色.
<5>.textAligment: 文本对齐方式. - 文字大小设置:
<1>.adjustsFontSizeToFitWidth: 根据宽度自动调整文字大小.
<2>.minimumFontSize: 适合文本的最小字体(当adjustsFontSizeToFitWidth=YES时启用). - 编辑行为:
<1>.clearsOnBeginEditing: 再次编辑的时候,是否清空上一次的内容. - 外观:
<1>. background: 背景图片.
<2>.disabledBackground: 背景图可以被拉伸.(background设置的情况下)
<3>.borderStyle: 边框样式.
typedef NS_ENUM(NSInteger, UITextBorderStyle) {
UITextBorderStyleNone, //默认样式,文本字段不显示边框。
UITextBorderStyleLine, //在文本字段周围显示一个细长的矩形。
UITextBorderStyleBezel, //显示文本字段的边框样式边框。此样式通常用于标准数据输入字段。
UITextBorderStyleRoundedRect (有圆角, 此时自定义的background图片 无效)
};
- 辅助视图:
<1>.leftView, rightView: 左右视图(和leftViewMode,rightViewMode配合使用).
<2>.leftViewMode,rightViewMode: 视图出现的模式.如下:
<3>.clearButtonMode: 清空小X出现的模式.如下:
typedef NS_ENUM(NSInteger, UITextFieldViewMode) {
UITextFieldViewModeNever, // 从不出现
UITextFieldViewModeWhileEditing, //编辑的时候出现
UITextFieldViewModeUnlessEditing, //不编辑的时候出现
UITextFieldViewModeAlways //总是出现
};
- 编辑样式:
<1>.secureTextEntry: 设置是否以密码的圆点形式显示.
<2>. autocorrectionType: 自动纠错联想,可以禁掉.
<3>.returnKeyType : 键盘上返回键的枚举和说明.
typedef NS_ENUM(NSInteger, UIReturnKeyType) {
UIReturnKeyDefault, //指定Return键的可见标题是“return”。
UIReturnKeyGo, //指定Return键的可见标题是“Go”。
UIReturnKeyGoogle, //指定返回键的可见标题是“Search”, 用谷歌搜索。
UIReturnKeyJoin, //指定Return键的可见标题是“Join”, 登录时使用。
UIReturnKeyNext, //指定Return键的可见标题是“Next”, 提示下一步。
UIReturnKeyRoute, //指定返回键的可见标题是“Route”, 路线。
UIReturnKeySearch, //指定返回键的可见标题是“Search”, 搜索
UIReturnKeySend, //指定返回键的可见标题是“Send”, 发送。
UIReturnKeyYahoo, //指定返回键的可见标题是“Yahoo”, 使用yahoo搜索。
UIReturnKeyDone, //指定返回键的可见标题是“Done”, 完成。
UIReturnKeyEmergencyCall, //指定返回键的可见标题是“EmergencyCall”, 紧急呼叫。
UIReturnKeyContinue NS_ENUM_AVAILABLE_IOS(9_0), //指定返回键的可见标题是“Continue”, 继续。
};
<4>. keyboardType: 弹出键盘样式.
typedef NS_ENUM(NSInteger, UIKeyboardType) {
UIKeyboardTypeDefault, // Default type for the current input method.
UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters
UIKeyboardTypeNumbersAndPunctuation, // 制定数字和标点键盘
UIKeyboardTypeURL, // 指定为URL输入优化的键盘。这种键盘类型显着地以周期(“ .”)和斜杠(“/”)字符和“ .com”字符串为特征, space键改成了三个键".", "/"", .com"
UIKeyboardTypeNumberPad, // 指定用于PIN输入的数字小键盘。这种键盘类型0通过突出显示数字9。此键盘类型不支持自动大写。
UIKeyboardTypePhonePad, // 指定用于输入电话号码的键盘。这种键盘类型显着地0通过数字9和“ *”和“ #” "," ";" "+"字符。此键盘类型不支持自动大写。
UIKeyboardTypeNamePhonePad, // 指定用于输入个人姓名或电话号码的小键盘。此键盘类型不支持自动大写。
UIKeyboardTypeEmailAddress, //指定为输入电子邮件地址而优化的键盘。这种键盘类型突出地表现了at(“ @”),句号(“ .”)和空格字符。
UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1), // 指定一个带有数字和小数点的键盘.
UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0), // 指定一个针对Twitter文本输入进行优化的键盘,可以方便地访问at(“ @”)和hash(“ #”)字符。
UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0), // 指定针对网页搜索字词和网址输入进行优化的键盘。这种类型的键盘突出显示了空格和句点(“ .”)字符。
UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0), // 指定只输出ASCII数字的数字键盘。
UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // 指定为字母输入而优化的键盘。
};
<5>.keyboardAppearance: 键盘外观.
typedef NS_ENUM(NSInteger, UIKeyboardAppearance) {
UIKeyboardAppearanceDefault, // Default apperance for the current input method.
UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),
UIKeyboardAppearanceAlert = UIKeyboardAppearanceDark, // Deprecated
};
<6>.inputAccessoryView: UITextField和UITextViews有一个inputAccessoryView的属性,当展示一个自定义的view的时候.设置此属性,该view就会在键盘上面一起显示.通常是tooBarView. 详细介绍:inputAccessoryView和inputView
<7>.inputView: 就是显示键盘的View,如果重写这个方法则不再弹出键盘,代替的使我们自定义的view.详细介绍:inputAccessoryView和inputView
<8>. 键盘相关通知:
通知名称 | 说明 |
---|---|
UIKeyboardWillShowNotification | 键盘将要显示时发出的通知 |
UIKeyboardDidShowNotification | 键盘已经显示发出的通知 |
UIKeyboardWillHideNotification | 键盘将要隐藏时发出的通知 |
UIKeyboardDidHideNotification | 键盘已经隐藏时发出的通知 |
UIKeyboardWillChangeFrameNotification | 键盘的大小即将发生改变时发出的通知 |
UIKeyboardDidChangeFrameNotification | 键盘的大小已经发生改变时发出的通知 |
<9>.contentHorizontalAlignment\contentVerticalAlignment:内容的水平垂直显示模式.
typedef NS_ENUM(NSInteger, UIControlContentVerticalAlignment) {//垂直方向
UIControlContentVerticalAlignmentCenter = 0,
UIControlContentVerticalAlignmentTop = 1,
UIControlContentVerticalAlignmentBottom = 2,
UIControlContentVerticalAlignmentFill = 3,
};
typedef NS_ENUM(NSInteger, UIControlContentHorizontalAlignment) {//水平方向
UIControlContentHorizontalAlignmentCenter = 0,
UIControlContentHorizontalAlignmentLeft = 1,
UIControlContentHorizontalAlignmentRight = 2,
UIControlContentHorizontalAlignmentFill = 3,
};
<10>.autoresizingMask: 视图的布局. UIViewAutoresizingFlexibleHeight:自适应高度.
typedef NS_OPTIONS(NSUInteger, UIViewAutoresizing) {
UIViewAutoresizingNone = 0, // 不随父视图的改变而改变
UIViewAutoresizingFlexibleLeftMargin = 1 << 0, // 自动调整view与父视图左间距,保证右间距不变
UIViewAutoresizingFlexibleWidth = 1 << 1, // 自动调整view的宽度,保证左间距和右间距不变
UIViewAutoresizingFlexibleRightMargin = 1 << 2, // 自动调整view与父视图右间距,以保证左间距不变
UIViewAutoresizingFlexibleTopMargin = 1 << 3, // 自动调整view与父视图上间距,以保证下间距不变
UIViewAutoresizingFlexibleHeight = 1 << 4, // 自动调整view的高度,以保证上间距和下间距不变
UIViewAutoresizingFlexibleBottomMargin = 1 << 5 // 自动调整view与父视图的下间距,以保证上间距不变
};
<11>.autocapitalizationType: 字母大写的设置.
typedef NS_ENUM(NSInteger, UITextAutocapitalizationType) {
UITextAutocapitalizationTypeNone, //不自动大写
UITextAutocapitalizationTypeWords, //单词首字母大写
UITextAutocapitalizationTypeSentences, //句子的首字母大写
UITextAutocapitalizationTypeAllCharacters, //所有字母都大写
};
绘制和定位覆盖几个方法, 可以根据需求自己定制一个UITextField.
- (CGRect)borderRectForBounds:(CGRect)bounds;
- (CGRect)textRectForBounds:(CGRect)bounds;
- (CGRect)placeholderRectForBounds:(CGRect)bounds; 控制placeHolder的位置
- (CGRect)editingRectForBounds:(CGRect)bounds;
- (CGRect)clearButtonRectForBounds:(CGRect)bounds;
- (CGRect)leftViewRectForBounds:(CGRect)bounds;
- (CGRect)rightViewRectForBounds:(CGRect)bounds;
- (void)drawTextInRect:(CGRect)rect;
- (void)drawPlaceholderInRect:(CGRect)rect;
代理方法:
- 当开始点击textField会调用的方法,状态是成为第一响应者之前, 可以控制是否能编辑.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
- textField成为第一响应者.
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
//通知
UITextFieldTextDidBeginEditingNotification;
- textField成为第一响应者之后,会显示键盘和输入视图.此时通知:UIKeyboardWillShowNotification, UIKeyboardDidShowNotification , UIKeyboardWillChangeFrameNotification , UIKeyboardDidChangeFrameNotification
- textField在编辑期间:
每当文本改变就会调用.代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
//通知:
UITextFieldTextDidEndEditingNotification;
//当用户点击内置按钮清除文本时:
- (BOOL)textFieldShouldClear:(UITextField *)textField; // called when clear button pressed. return NO to ignore (no notifications)
//它调用用户点击键盘的返回按钮时的方法。
- (BOOL)textFieldShouldReturn:(UITextField *)textField; // called when 'return' key pressed. return NO to ignore.
- 当textField编辑结束时调用的方法
//将要结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField; // return YES to allow editing to stop and to resign first responder status. NO to disallow the editing session to end
//结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField; // may be called if forced even if shouldEndEditing returns NO (e.g. view removed from window) or endEditing:YES called
//结束编辑并给出结束的原因.1.提交.2.取消
- (void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason NS_AVAILABLE_IOS(10_0); // if implemented, called in place of textFieldDidEndEditing:
//键盘通知
UIKeyboardWillHideNotification和UIKeyboardDidHideNotification
//textField通知
TextDidEndEditingNotification
- 点击空白处回收键盘.不是代理的方法.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
自定义键盘
UIView *redView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 100)];
redView.backgroundColor = [UIColor redColor];
textField.inputView = redView;
textField.textColor = [UIColor redColor];