UITextField
1 @synthesize textField = _textField; 2 3 4 - (void)viewDidLoad { 5 [super viewDidLoad]; 6 7 self.textField = [[UITextField alloc]init]; 8 9 self.textField.frame = CGRectMake(100, 100, 180, 40); 10 11 self.textField.text = @"UserName"; 12 13 self.textField.font = [UIFont systemFontOfSize:15]; 14 15 self.textField.textColor = [UIColor blackColor]; 16 17 //边框风格 18 self.textField.borderStyle = UITextBorderStyleBezel; 19 20 //设置虚拟键盘风格 21 //self.textField.keyboardType = UIKeyboardTypeDefault; 22 23 self.textField.keyboardType = UIKeyboardTypeNumberPad; 24 25 26 //设置文字提示信息,默认浅灰色,当文本框内什么都没有才显示 27 self.textField.placeholder = @"Input username"; 28 29 //是否作为密码输入 30 self.textField.secureTextEntry = YES; 31 32 [self.view addSubview:self.textField]; 33 } 34 35 //在屏幕空白处点击 36 -(void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 37 { 38 //不再作为消息的第一响应者 39 [self.textField resignFirstResponder]; 40 }
@interface ViewController : UIViewController<UIAlertViewDelegate> { UITextField* _textField; } //属性定义 @property(retain,nonatomic) UITextField* textField;