iOS开发-UI (五)UITextField
UITextField使用
1.创建方式
例:
UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, 30)];
2.常用方法和属性
1)边框样式
@property(nonatomic) UITextBorderStyle borderStyle;
UITextBorderStyleNone 没有边框,背景默认为透明
UITextBorderStyleLine 线框,背景默认为透明
UITextBorderStyleBezel bezel 风格边框,背景默认为透明
UITextBorderStyleRoundedRect 圆角边框,背景默认为白色
textField.borderStyle = UITextBorderStyleBezel;
2)提示文字: placeholder
textField.placeholder = @"请输入银行卡密码";
3)键盘类型: keyboardType
textField.keyboardType = UIKeyboardTypeNumberPad;
4)键盘样式: keyboardAppearance
textField.keyboardAppearance = UIKeyboardAppearanceLight;
5)密文输入: secureTextEntry
textField.secureTextEntry = YES;
6)再次编辑是否清空: clearsOnBeginEditing
textField.clearsOnBeginEditing = YES;
7)文本横向对齐方式: textAlignment
textField.textAlignment = NSTextAlignmentRight;
8)文本滚动: adjustsFontSizeToFitWidth
搭配 minimumFontSize一起使用
//回收键盘
[self.view endEditing: YES];
9)return键类型:returnKeyType
@property(nonatomic) UIReturnKeyType returnKeyType;
UIReturnKeyDefault,
UIReturnKeyGo,
UIReturnKeyGoogle,
UIReturnKeyJoin,
UIReturnKeyNext,
UIReturnKeyRoute,
UIReturnKeySearch,
UIReturnKeySend,
UIReturnKeyYahoo,
UIReturnKeyDone,
UIReturnKeyEmergencyCall,
10)清理按钮模式:clearButtonMode
@property(nonatomic) UITextFieldViewMode clearButtonMode;
UITextFieldViewModeNever,
UITextFieldViewModeWhileEditing,
UITextFieldViewModeUnlessEditing,
UITextFieldViewModeAlways
3.UITextFieldDelegate 协议
1)是否可以进入编辑模式
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField;
//返回NO,无法进入编辑状态
return YES;
2)文本框已经进入编辑模式
-(void)textFieldDidBeginEditing:(UITextField *)textField;
3)文本框是否可以结束编辑模式
-(BOOL)textFieldShowEndEditing:(UITextField *)textField;
//返回NO,无法结束编辑状态
return YES;
4)文本框已结束编辑模式
-(void)textFieldDidEndEditing:(UITextField *)textField;
5)是否可以点击clear按钮
-(BOOL)textFieldShouldClear:(UITextField *)textField;
//返回NO,点击clear按钮无响应
return YES;
6)是否可以点击return按钮
-(BOOL)textFieldShouldReturn:(UITextField *)textField;
//移除第一响应者
[textField resignFirstResponder];
return YES;
7)允许修改内容
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; 例如: if (textField.text.length >= 6) { if ([string isEqualToString:@""]) { return YES; } return NO; } return YES; }