UITextField *tf = [[UITextField alloc]init];
tf.frame = CGRectMake(40, 150, 250, 40);
tf.backgroundColor = [UIColor lightGrayColor];
//*****************2.方法屬性:***********************
//設置邊框樣式
// UITextBorderStyleNone, --無
// UITextBorderStyleLine, --線框
// UITextBorderStyleBezel, --陰影
// UITextBorderStyleRoundedRect --圓角
tf.borderStyle = UITextBorderStyleRoundedRect;
//設置密文輸入
tf.secureTextEntry = YES;
//設置提示(佔位符)
tf.placeholder = @"請輸入密碼";
//設置清除按鈕模式
// UITextFieldViewModeNever, --從不顯示
// UITextFieldViewModeWhileEditing, --正在編輯的時候顯示
// UITextFieldViewModeUnlessEditing, --非編輯時候顯示
// UITextFieldViewModeAlways --總是顯示
tf.clearButtonMode = UITextFieldViewModeUnlessEditing;
//設置字體
tf.font = [UIFont systemFontOfSize:26];
//顯示文字顏色
tf.textColor = [UIColor redColor];
//設置鍵盤顯示樣式
// UIKeyboardTypeDefault, // Default type for the current input method.
// UIKeyboardTypeASCIICapable, // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
// UIKeyboardTypeNumbersAndPunctuation, // Numbers and assorted punctuation.
// UIKeyboardTypeURL, // A type optimized for URL entry (shows . / .com prominently).
// UIKeyboardTypeNumberPad, // A number pad (0-9). Suitable for PIN entry.
// UIKeyboardTypePhonePad, // A phone pad (1-9, *, 0, #, with letters under the numbers).
// UIKeyboardTypeNamePhonePad, // A type optimized for entering a person's name or phone number.
// UIKeyboardTypeEmailAddress, // A type optimized for multiple email address entry (shows space @ . prominently).
// UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1), // A number pad with a decimal point.
// UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0), // A type optimized for twitter text entry (easy access to @ #)
// UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0), // A default keyboard type with URL-oriented addition (shows space . prominently).
tf.keyboardType = UIKeyboardTypePhonePad;
//設置鍵盤的外觀
// UIKeyboardAppearanceDefault, // Default apperance for the current input method.
// UIKeyboardAppearanceDark NS_ENUM_AVAILABLE_IOS(7_0),
// UIKeyboardAppearanceLight NS_ENUM_AVAILABLE_IOS(7_0),
tf.keyboardAppearance = UIKeyboardAppearanceDark;
//設置自動調整字體大小來適應寬度(但是有最小極限,過了還是會往後移)
tf2.adjustsFontSizeToFitWidth = YES;
//調整能顯示的最小字體
tf2.minimumFontSize = 20;
//設置或者獲取文本框的內容
tf2.text= @"我是文本輸入框";
NSString *text = tf2.text;
//設置文本對齊方式
tf2.textAlignment = NSTextAlignmentCenter;
//設置是否在開始編輯時清空內容
tf2.clearsOnBeginEditing = YES;
//判斷當前是否在編輯狀態
//tf2.isEditing;
//設置文本輸入框的左視圖
UIView *view = [[UIView alloc]init];
//設置左視圖的時候X,Y座標無效,高度在tf2文本輸入框的範圍內有效
view.frame = CGRectMake(100, 100, 100, 100);
view.backgroundColor = [UIColor purpleColor];
tf2.leftView = view;
//想要顯示左視圖必須設置視圖的顯示模式
tf2.leftViewMode = UITextFieldViewModeAlways;
//設置文本輸入框的右視圖
UIView *view1 = [[UIView alloc]init];
//設置左視圖的時候X,Y座標無效,高度在tf2文本輸入框的範圍內有效
view1.frame = CGRectMake(100, 100, 20, 20);
view1.backgroundColor = [UIColor orangeColor];
tf2.rightView = view1;
//想要顯示左視圖必須設置視圖的顯示模式
tf2.rightViewMode = UITextFieldViewModeAlways;
//設置文本輸入框的右視圖
UIView *inputView = [[UIView alloc]init];
//設置左視圖的時候X,Y座標無效,高度在tf2文本輸入框的範圍內有效
inputView.frame = CGRectMake(100, 100, 100, 100);
inputView.backgroundColor = [UIColor orangeColor];
//設置響應鍵盤的來源(自定義軟鍵盤)
//tf2.inputView = inputView;
//添加附屬視圖(在軟鍵盤上面多顯示一層內容)(二級視圖)
UIView *inputView2 = [[UIView alloc]init];
inputView2.frame = CGRectMake(100, 100, 100, 100);
inputView2.backgroundColor = [UIColor brownColor];
tf2.inputAccessoryView = inputView2;
//設置返回按鈕的類型
tf2.returnKeyType = UIReturnKeyGo;
//***********************************3. 爲這個文本框控件設置代理***********************************
//這個控制器必須遵守協議 .h: <UIXXXXXXDalegate>
tf.delegate = self;
//***********************************4. 調用代理的方法實現對應功能***********************************
#pragma mark - UITextFieldDelegate方法
/**
* 這個方法返回是否可以開始編輯,當用戶點擊textfield的時候調用這個方法
*
* @param textField 當前被點擊的文本輸入框對象
*
* @return YES代表可以編輯,NO則不能編輯
*/
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"%@",NSStringFromSelector(_cmd));
return YES;
}
/**
* 這個方法在開始編輯時調用(進入編輯狀態時執行)
*
* @param textField 當前被點擊的文本輸入框對象
*/
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"%@",NSStringFromSelector(_cmd));
if (textField.tag == 102) {
NSLog(@"tf2");
}
}
/**
* 詢問是否可以結束編輯狀態(textField想要結束編輯狀態時調用)
*
* @param textField 當前被點擊的文本輸入框對象
*
* @return YES:可以結束 NO:不能結束
*/
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
/**
* 已經結束編輯狀態調用該方法
*
* @param textField 當前編輯文本框對象
*/
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"已經結束");
}
//*****************************鍵盤收回方法********************************
//監聽鍵盤彈出的通知
//當軟鍵盤彈出前,系統會自動發送通知信息
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillShow:) name: UIKeyboardWillShowNotification object:nil];
//監聽鍵盤收回的通知
//當軟鍵盤收回前,系統會自動發送通知信息
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(keyboardWillHide:) name: UIKeyboardWillHideNotification object:nil];
/**
* 處理鍵盤收回前通知事件
*
* @param not <#not description#>
*/
- (void)keyboardWillHide:(NSNotification *)not
{
//1.取到兩個按鈕
for (int i=0; i<2; i++) {
UIButton *btn = (UIButton *)[self.view viewWithTag:1000+i];
CGRect rect = btn.frame;
rect.origin.y += 100;
//調用動畫效果
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8) {
}else{
[UIView animateWithDuration:1 animations:^{
btn.frame = rect;
}];
}
}
}
/**
* 處理鍵盤彈出前通知事件
*
* @param not 接收返回的通知參數
*/
- (void)keyboardWillShow:(NSNotification *)not
{
//1.取到兩個按鈕
for (int i=0; i<2; i++) {
UIButton *btn = (UIButton *)[self.view viewWithTag:1000+i];
CGRect rect = btn.frame;
rect.origin.y -= 100;
//調用動畫效果
if ([[[UIDevice currentDevice] systemVersion] floatValue] < 8) {
}else{
[UIView animateWithDuration:1 animations:^{
btn.frame = rect;
}];
}
}
}
//創建提示標籤
- (void)createLabels
{
NSArray *array = @[@"Q Q :",@"密碼 :"];
for (int i=0; i<array.count; i++) {
UILabel *label = [[UILabel alloc]init];
label.frame = CGRectMake(40, 100 + i*150, 80, 40);
label.text = array[i];
[self.view addSubview:label];
}
}
//創建
- (void)createField1
{
UITextField *tf1 = [[UITextField alloc]init];
tf1.frame = CGRectMake(130, 100, 200, 40);
tf1.borderStyle = UITextBorderStyleRoundedRect;
tf1.keyboardType = UIKeyboardTypeNumberPad;
tf1.font = [UIFont systemFontOfSize:26];
tf1.placeholder = @"請輸入賬號";
tf1.delegate = self;
tf1.tag = 101;
tf1.clearButtonMode = UITextFieldViewModeAlways;
[self.view addSubview:tf1];
}
//創建
- (void)createField2
{
UITextField *tf2 = [[UITextField alloc]init];
tf2.frame = CGRectMake(130, 250, 200, 40);
tf2.borderStyle = UITextBorderStyleRoundedRect;
//tf2.keyboardType = UIKeyboardTypeNumberPad;
tf2.font = [UIFont systemFontOfSize:26];
tf2.placeholder = @"請輸入密碼";
tf2.secureTextEntry = YES;
tf2.delegate = self;
tf2.tag = 102;
tf2.returnKeyType = UIReturnKeyGo;
tf2.clearButtonMode = UITextFieldViewModeAlways;
[self.view addSubview:tf2];
}
- (void)createBtn
{
NSArray *arr = @[@"登 錄",@"注 冊"];
for (int i=0; i<arr.count; i++) {
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(80 + i*100, 500, 100, 40);
[btn setTitle:arr[i] forState:UIControlStateNormal];
btn.titleLabel.font = [UIFont systemFontOfSize:26];
btn.tag = 1000 + i;
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
}
}
- (void)btnClick:(UIButton *)btn
{
}
/**
* 收回軟鍵盤方法4:
* 從UIControl繼承可以創建自定義的視圖
*/
- (void)createController
{
UIControl *ctrl = [[UIControl alloc]init];
ctrl.frame = [[UIScreen mainScreen]bounds];
[ctrl addTarget:self action:@selector(ctrlClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:ctrl];
[self.view sendSubviewToBack:ctrl];
}
/**
* ctrl點擊事件,去除所有空間第一響應者
*/
- (void)ctrlClicked
{
[self.view endEditing:YES];
}
#pragma mark - 觸摸相關方法
/**
* 收回軟鍵盤的方式2
*
* @param touches <#touches description#>
* @param event <#event description#>
*/
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//讓所有的文本輸入框成爲第一響應者
// for (int i=0; i<2; i++) {
// UITextField *tf = (UITextField *)[self.view viewWithTag:101+i];
// [tf resignFirstResponder];
// }
//方法3:讓VIEW上所有的子視圖結束編輯(取消成爲第一響應者)
[self.view endEditing:YES];
}
#pragma mark - UITextFieldDelegate方法
/**
* 當用戶點擊清楚按鈕時調用
*
* @param textField <#textField description#>
*
* @return YES:可以清除 NO:不能清除
*/
- (BOOL)textFieldShouldClear:(UITextField *)textField
{
if (textField.tag == 101) {
//清除按鈕內容
//textField.text = @"";
return NO;
}else{
return YES;
}
}
/**
* 設置返回鍵是否可以返回(常在這裏設置收回軟鍵盤),當用戶點擊返回按鈕時被調用
*
* @param textField 當前被點擊的文本輸入框對象
*
* @return YES:可以返回了 NO:不可以返回
*/
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
//收回軟鍵盤的方式1:textField會自動退出編輯模式,並且收回軟鍵盤
[textField resignFirstResponder];
return YES;
}
/**
* 這個方法在用戶點擊軟鍵盤,textField將用哦過戶輸入數據放入textField之前調用
* (可以在這裏處理用戶輸入的信息做出判斷)
* @param textField 當前被點擊的文本輸入框對象
* @param range 需要替換的字符串範圍
* @param string 用戶輸入的字符
*
* @return YES:用戶輸入內容添加到textField中,NO:不添加
*/
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string isEqualToString:@"0"]) {
return NO;
}
NSLog(@"rang = %@,string = %@",NSStringFromRange(range),string);
return YES;
}
/**
* 這個方法返回是否可以開始編輯,當用戶點擊textfield的時候調用這個方法
*
* @param textField 當前被點擊的文本輸入框對象
*
* @return YES代表可以編輯,並且系統讓這個textField成爲第一響應者,進入編輯狀態,並且彈出軟鍵盤;NO則不能編輯
*/
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return YES;
}
/**
* 這個方法在開始編輯時調用(進入編輯狀態時執行)
*
* @param textField 當前被點擊的文本輸入框對象
*/
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"%@",NSStringFromSelector(_cmd));
if (textField.tag == 102) {
NSLog(@"tf2");
}
}
/**
* 詢問是否可以結束編輯狀態(textField想要結束編輯狀態時調用)
*
* @param textField 當前被點擊的文本輸入框對象
*
* @return YES:可以結束 NO:不能結束
*/
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
return YES;
}
/**
* 已經結束編輯狀態調用該方法
*
* @param textField 當前編輯文本框對象
*/
- (void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"已經結束");
}