简单看一下UITextField的属性
- (void)createTextField{
UITextField * textField = [[UITextField alloc] initWithFrame:CGRectMake(40, 40, 240, 40)];
//设置UITextField的边框风格,否则看不见textField 盲点的话可以点到它
/*
UITextBorderStyleRoundedRect 圆角
UITextBorderStyleBezel 上、左有边框
UITextBorderStyleLine 边框就是一个矩形框 背景还是父视图的背景色
UITextBorderStyleNone 默认的没有边框
*/
textField.borderStyle = UITextBorderStyleRoundedRect;
//设置占位符 也可以叫提示语句
textField.placeholder = @"请输入:";
//textfield 最后面的一个删除按钮显示模式
/*
UITextFieldViewModeAlways 开始没有 输入后就一直显示
UITextFieldViewModeNever 什么时候也不会显示
UITextFieldViewModeUnlessEditing
UITextFieldViewModeWhileEditing 输入时显示
*/
textField.clearButtonMode = UITextFieldViewModeAlways;
//设置键盘的风格(数字键盘、26字母键盘等等) UIKeyboardTypeNumberPad数字键盘
textField.keyboardType = UIKeyboardTypeNumberPad;
//设置return的样式 即键盘上右下角的按键
textField.returnKeyType = UIReturnKeyDone;
//设置textfield的默认文本文字
textField.text = @"Snail";
//当编辑完后 再一次编辑时 是否清空里面的内容 YES:清空
textField.clearsOnBeginEditing = YES;
//判断textfield是否正在编辑
BOOL ret = textField.editing;
//textfield是否可以被编辑 YES:可编辑
textField.enabled = NO;
//输入的文本是否隐藏(password)
textField.secureTextEntry = YES;
//设置textfield的leftView、rightView x、y的值不会影响leftView的位置 只与width、height有关
UIView * leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 40)];
leftView.backgroundColor = [UIColor redColor];
textField.leftView = leftView;
//mode必须设置 否则将不会显示在textField上面
textField.leftViewMode = UITextFieldViewModeAlways;
[self.view addSubview:textField];
}
效果图如下