//TextField 是UIControl的子类, UIControl又是UIView的子类, 所以也是视图, 只不过比UIView多了两个功能:1 文字显示 2 文本编辑 也是分为四步:1 创建对象 2 配置属性 3 添加到父视图 4 释放所有权
- (void)creatTextField
{
//创建对象
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(160 - 75, 120, 150, 40)];
//设置属性
textField.backgroundColor = [UIColor whiteColor];
//1 设置textField的样式 UITextBorderStyleRoundedRect圆角矩形
textField.borderStyle = UITextBorderStyleRoundedRect;
//2 设置textField显示的提示文字, 但是不作为文本的内容
textField.placeholder = @"用户名/邮箱";
//3 设置textField开始显示的文字
textField.text = @"username";
//4 设置文本的颜色
textField.textColor = [UIColor redColor];
//5 设置文本的对齐方式
textField.textAlignment = NSTextAlignmentLeft;
//6 设置文本的字体
textField.font = [UIFont fontWithName:@"Courier New" size:20];
//7 设置输入框是否可编辑
textField.enabled = YES;
//8 设置当开始编辑时, 是否清楚输入框的内容
textField.clearsOnBeginEditing = YES;
//9 设置密码模式, 输入框中的内容是否以点的形式显示
textField.secureTextEntry = YES;
//10 设置弹出键盘的样式
// textField.keyboardType = UIKeyboardTypeNumberPad;
//11 键盘右下角显示的样式
textField.returnKeyType = UIReturnKeyDone;
//12 代理 使用步骤
//1> 设置代理
//2> 服从协议
//3> 实现协议中的方法
textField.delegate = self;//self是当前类对象(CCLAppDelegate)
//13 inputView
UIView *inputView = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 320, 40)];
inputView.backgroundColor = [UIColor whiteColor];
// textField.inputView = inputView;
//14
textField.inputAccessoryView = inputView;
//添加到父视图
[_bgView addSubview:textField];
//释放
[textField release];
}
UITextField基本用法
最新推荐文章于 2018-11-17 21:20:58 发布