- (void)loadView
{
[super loadView];
// 将视图转化成UIScrollView, 才能让视图在键盘出现时发生偏移
self.view = [[UIScrollView alloc] initWithFrame:self.view.frame];
self.view.backgroundColor = [UIColor grayColor];
self.view.layer.borderWidth = 2.0;
self.view.layer.borderColor = [UIColor redColor].CGColor;
self.view.layer.masksToBounds = YES;
// 创建文本框
UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 10.0, 300.0, 30.0)];
// 边框样式
textField.borderStyle = UITextBorderStyleRoundedRect;
// 背景颜色
textField.backgroundColor = [UIColor whiteColor];
// 背景图案
textField.background = [UIImage imageNamed:@"bn280x80a.png"];
// 没有内容时, 显示的提示语
textField.placeholder = @"水印文字";
// 字体样式
textField.font = [UIFont fontWithName:@"Arial" size:12.0];
// 字体颜色
textField.textColor = [UIColor redColor];
// 这是一个密码框, 因为影响测试, 暂时注释掉
//textField.secureTextEntry = YES;
// 是否纠错
textField.autocorrectionType = UITextAutocorrectionTypeNo;
// 开始编辑时, 清空
textField.clearsOnBeginEditing = YES;
// 内容的水平对齐方式
textField.textAlignment = NSTextAlignmentRight;
// 内容的垂直对齐方式, UITextField继承自UIControl, 此类中有一个属性contentVerticalAlignment
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
// 首字母是否大写
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
// 设置为YES时, 文本会自动缩小以适应文本窗口大小. 设置为NO时(默认), 保持原来大小, 而让长文本滚动
textField.adjustsFontSizeToFitWidth = YES;
// 设置自动缩小显示的最小字体大小, 与adjustsFontSizeToFitWidth配合使用
textField.minimumFontSize = 8.0;
// 最右侧加图片是以下代码, 左侧类似, 会占用清除按钮的位置
UIImageView* imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, 16.0, 16.0)];
imageview.image = [UIImage imageNamed:@"bn001b.png"];
textField.rightView = imageview;
textField.rightViewMode = UITextFieldViewModeAlways;
// 清除按钮
textField.clearButtonMode = UITextFieldViewModeAlways;
// return键变成什么键
textField.returnKeyType = UIReturnKeyDone;
// 键盘样式
textField.keyboardType = UIKeyboardTypeDefault;
// 键盘外观
textField.keyboardAppearance = UIKeyboardAppearanceDefault;
// 代理UITextFieldDelegate协议
textField.delegate = self;
// 预置文字
textField.text = @"预置文字";
// 加入视图栈
[self.view addSubview:textField];
// 再创建几个文本框
for (int i = 0; i < 5; i++)
{
UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(10.0, 50.0+40.0*i, 300.0, 30.0)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.placeholder = [NSString stringWithFormat: @"第%d个文本框", i+1];
textField.delegate = self;
[self.view addSubview:textField];
}
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
CGRect begRect = [textField bounds];
CGRect endRect = [textField convertRect:begRect toView:self.view];
NSLog(@"%@ (%f,%f,%f,%f) to %@ (%f,%f,%f,%f)",
textField.class, begRect.origin.x, begRect.origin.y, begRect.size.width, begRect.size.height,
self.view.class, endRect.origin.x, endRect.origin.y, endRect.size.width, endRect.size.height);
float offsetY = endRect.origin.y - 60;
// 调整文本框位置
[(UIScrollView*)self.view setContentOffset:CGPointMake(0, offsetY) animated:YES];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
// 收起键盘
[textField resignFirstResponder];
// 或者 [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
[(UIScrollView*)self.view setContentOffset:CGPointMake(0.0, 0.0) animated:YES];
return YES;
}
No.09 Xcode(5.x) UITextField
最新推荐文章于 2023-07-14 01:46:15 发布