/*
UITextField文本輸入框
*/
UITextField * textField = [[UITextField alloc]initWithFrame:CGRectMake(50, 50, 275, 50)];
//設置邊框形式
/*
UITextBorderStyleRoundedRect 圓角形式
UITextBorderStyleLine 線條形式
UITextBorderStyleBezel 槽形式
*/
textField.borderStyle = UITextBorderStyleRoundedRect;
//通常用於尋找當前文本輸入框中顯示的文字
textField.text = @"";
//文本顏色
textField.textColor = [UIColor redColor];
//設置文本字體大小
textField.font = [UIFont systemFontOfSize:20];
//設置背景顏色
textField.backgroundColor = [UIColor lightGrayColor];
//當重復開始編輯時候 清除文字
textField.clearsOnBeginEditing = YES;
//文字提示
textField.placeholder = @"請輸入您的大區名字";
//文字密文(暗文) 該屬性通常用於設置密碼輸入框
textField.secureTextEntry = NO;
//文字輸入時的對齊方式
textField.textAlignment = NSTextAlignmentCenter;
//文字輸入的清除按鈕
/*
UITextFieldViewModeWhileEditing 當輸入時
UITextFieldViewModeAlways 總是
UITextFieldViewModeUnlessEditing 不在輸入時候
*/
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
//鍵盤的類型
textField.keyboardType = UIKeyboardTypeDefault;
//retuan鍵類型 可自定義鍵盤
textField.returnKeyType = UIReturnKeyJoin;
//左視圖
UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
label.text = @"賬號";
label.textAlignment = NSTextAlignmentCenter;
textField.leftView = label;
textField.leftViewMode = UITextFieldViewModeWhileEditing;
//右視圖
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:@"確定" forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, 50, 50);
[button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
textField.rightView = button;
textField.rightViewMode = UITextFieldViewModeAlways;
[self.window addSubview:textField];
//讓鍵盤產生第一響應 鍵盤會自動彈起
[textField becomeFirstResponder];
//收起鍵盤
/*
1、點擊鍵盤的return鍵
2、點擊Button
3、點擊空白處彈回鍵盤
*/
/*
手勢
*/
UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick)];
自定制方法/手勢方法
- (void)tapClick{
UITextField * textField = (UITextField*)[self.window viewWithTag:100];
[textField resignFirstResponder];
}
- (void)buttonClick:(UIButton*)button{
//取消第一響應
UITextField * textFiled = (UITextField*)[self.window viewWithTag:100];
[textFiled resignFirstResponder];
}
所有代理方法作用
//當Return鍵被點擊時調用 通常用於收回鍵盤
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;//5.1前設置NO為點擊無效
}
//文本輸入框開始輸入時調用
- (void)textFieldDidBeginEditing:(UITextField *)textField{
//將鍵盤彈出
NSLog(@"開始輸入");
}
//文本輸入框結束輸入時調用
- (void)textFieldDidEndEditing:(UITextField *)textField{
//獲取當前文本輸入框中所輸入的文字
NSLog(@"所輸入的內容為:%@",textField.text);
//例:判斷賬號書寫形式是否正確 如果不正確提示填寫錯誤 重新輸入
NSLog(@"結束輸入");
}
//文本輸入框內容發生變化即會調用的方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
/*
NSLog(@"內容:%@",textField.text);//獲取的是上一次所輸入內容
NSLog(@"Location:%lu Length:%lu",range.location,range.length);//范圍為當前文字的位置,長度為零
NSLog(@"==%@==",string);//實時獲取當前輸入的字符
*/
//需求 實時獲取當前文本框中的所有文字
NSString * resultStr = [textField.text stringByAppendingString:string];
NSLog(@"%@",resultStr);
//可在該方法中判斷所輸入文字是否正確
return YES;
}
//了解
//是否允許文本輸入框可以輸入
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
//是否允許文本輸入框結束
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
//在該方法中可以通過判斷文本長度限制鍵盤是否可以收回
return NO;
}
//是否允許被清除
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"文字被清除");
return YES;
}