文本框基础
textField1.borderStyle = UITextBorderStyleLine 或 UITextBorderStyleBezel 或 UITextBorderStyleRoundedRect 或 UITextBorderStyleNone 效果如下:
frame, background, textcolor, textAlignment, font , text 一些属性
UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField; // return NO to disallow editing.
- (void)textFieldDidBeginEditing:(UITextField *)textField; // became first responder
- (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
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string; // return NO to not change text
- (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.
通知:
UIKIT_EXTERN NSString *const UITextFieldTextDidBeginEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidEndEditingNotification;
UIKIT_EXTERN NSString *const UITextFieldTextDidChangeNotification;
[[NSNotificationCenter defaultCenter] postNotificationName:UITextFieldTextDidBeginEditingNotification object:nil userInfo:nil];
一些例子:
textFields_ = [[NSArray alloc] initWithObjects:textField1, textField2, textField3, textField4, nil];
- (void)textFieldDidBeginEditing:(UITextField*)textField {
currentFieldIndex_ = [textFields_ indexOfObject:textField];
}
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
if ( textFields_.count <= ++currentFieldIndex_ ) {
currentFieldIndex_ = 0;
}
UITextField* newField = [textFields_ objectAtIndex:currentFieldIndex_];
if ( [newField canBecomeFirstResponder] ) {
[newField becomeFirstResponder];
}
return YES;
}
背景图片
//导入背景图片,并设置成自动伸缩
UIImage* imageWhitePaper = [UIImage imageNamed:@"paper.png"];
UIImage* stretchableWhitePaper = [imageWhitePaper stretchableImageWithLeftCapWidth:20 topCapHeight:20];
UIImage* imageGrayPaper = [UIImage imageNamed:@"paperGray.png"];
UIImage* stretchableGrayPaper = [imageGrayPaper stretchableImageWithLeftCapWidth:20 topCapHeight:20];
//创建UITextField实例
UITextField* textField = [[[UITextField alloc] init] autorelease];
textField.delegate = self;
textField.frame = CGRectMake( 20, 100, 280, 50 );
textField.background = stretchableWhitePaper;//设置背景图片
textField.disabledBackground = stretchableGrayPaper;//一般是为nil, 如果没有设置背景
textField.text = @"有图片";
textField.textAlignment = UITextAlignmentCenter;
textField.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
[self.view addSubview:textField];
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
textField.enabled = NO;
return YES;
}
没走此方法前 能编辑 有背景textField左右图片
UIImage* imageForLeft = [UIImage imageNamed:@"leftDog.png"];
UIImageView* imageViewForLeft = [[[UIImageView alloc] initWithImage:imageForLeft] autorelease];
UIImage* imageForRight = [UIImage imageNamed:@"rightDog.png"];
UIImageView* imageViewForRight = [[[UIImageView alloc] initWithImage:imageForRight] autorelease];
UITextField* textField1 = [[[UITextField alloc] init] autorelease];
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.frame = CGRectMake( 20, 30, 280, 50 );
textField1.text = @"一直在左右显示图片";
textField1.textAlignment = UITextAlignmentCenter;
textField1.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;//如何定位内容垂直内部控制 默认为中
textField1.leftView = imageViewForLeft;//输入框左侧追加UIImageView
textField1.rightView = imageViewForRight;//输入框右侧追加UIImageView
textField1.leftViewMode = UITextFieldViewModeAlways;//让左侧UIView一直显示
textField1.rightViewMode = UITextFieldViewModeAlways;//让右侧UIView一直显示
[self.view addSubview:textField1];
UITextField* textField2 = [[[UITextField alloc] init] autorelease];;
textField2.borderStyle = UITextBorderStyleRoundedRect;
textField2.frame = CGRectMake( 20, 100, 280, 50 );
textField2.text = @"非编辑状态时右侧显示详细按钮";
textField2.contentVerticalAlignment = UIControlContentHorizontalAlignmentCenter;
UIButton* button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
textField2.rightView = button;//输入框的右侧追加详细按钮
textField2.rightViewMode = UITextFieldViewModeUnlessEditing;//只在非编辑模式下才显示
[self.view addSubview:textField2];