#import "ViewController.h" @interface ViewController () @property (nonatomic, weak) UITextField *_textName; @property (nonatomic, weak) UITextField *_textPassword; @endhttp://www.cnblogs.com/pocket-mood/p/4331303.html @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; /** QQ */ UILabel *labName = [[UILabel alloc] init]; labName.frame = CGRectMake(15, 71, 40, 15); labName.text = @"QQ :"; labName.textColor = [UIColor blueColor]; [self.view addSubview:labName]; /** 密碼 */ UILabel *labPassword = [[UILabel alloc] init]; labPassword.frame = CGRectMake(15, 116, 40, 15); labPassword.text = @"密碼:"; labPassword.textColor = [UIColor redColor]; [self.view addSubview:labPassword]; /** QQ文本 */ UITextField *textName = [[UITextField alloc] init]; textName.frame = CGRectMake(60, 55, 260, 40); // 設置文本框樣式(圓角矩形) textName.borderStyle = UITextBorderStyleRoundedRect; // 設置文本框在沒內容時顯示的佔位格內容 textName.placeholder = @"請輸入QQ號"; textName.textColor = [UIColor blackColor]; // 設置鍵盤爲數字鍵盤輸入 textName.keyboardType = UIKeyboardTypeNumberPad; // 設置編輯時右側出現所有刪除圖標 textName.clearButtonMode = UITextFieldViewModeWhileEditing; [self.view addSubview:textName]; // 爲了使_textName之後能夠調用控件屬性 self._textName = textName; /** 密碼文本 */ UITextField *textPassword = [[UITextField alloc] init]; textPassword.frame = CGRectMake(60, 100, 260, 40); textPassword.borderStyle = UITextBorderStyleRoundedRect; textPassword.placeholder = @"請輸入密碼"; textPassword.textColor = [UIColor redColor]; // 設置文本爲密碼狀態,文本所有轉化爲點顯示 textPassword.secureTextEntry = YES; textPassword.clearButtonMode = UITextFieldViewModeWhileEditing; [self.view addSubview:textPassword]; self._textPassword = textPassword; /** 登陸按鈕 */ UIButton *btnRegiste = [[UIButton alloc] init]; btnRegiste.frame = CGRectMake(15, 290, 300, 60); [btnRegiste setTitle:@"登陸" forState:UIControlStateNormal]; [btnRegiste setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal]; [btnRegiste setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted]; [btnRegiste setBackgroundColor:[UIColor blueColor]]; // 設置Button按鈕文本字體樣式和大小 btnRegiste.titleLabel.font = [UIFont fontWithName:@"Arial" size:28.0]; // 監聽事件的觸發 [btnRegiste addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btnRegiste]; } // 監聽 - (void) btnClick: (UIButton *) button { NSLog(@"----登陸----\nQQ:%@\n密碼:%@",self._textName.text,self._textPassword.text); // 退出鍵盤狀態 [self.view endEditing:YES]; } @end