UITextField是輸入字符的視圖控件
1是UIView的子類,具有與view一樣的屬性
2字符輸入只能是單行輸入,不能換行,也不能多行輸入
3與自己特有的屬性
4 textfield通常要設置其代理,並實現相應的代理方法
5主要使用場景,如:登錄頁面中輸入帳號密碼,或輸入手機號,或輸入昵稱等方面
6使用注意事項:
(1)輸入鍵盤的顯示,以及隱藏;
(2)輸入時避免輸入框被鍵盤遮擋;
- //實例化
- UITextField*textfield001=[[UITextFieldalloc]initWithFrame:CGRectMake(10.0,50.0,200.0,30.0)];
-
- //添加到父視圖
- [self.viewaddSubview:textfield001];
-
- //設置背景顏色
- textfield001.backgroundColor=[UIColorredColor];
-
- //設置邊框屬性
- textfield001.layer.cornerRadius=10.0;
- textfield001.layer.masksToBounds=YES;
- textfield001.layer.borderColor=[UIColoryellowColor].CGColor;
- textfield001.layer.borderWidth=1.0;
- //字體設置
- //占位符,即提示信息
- textfield001.placeholder=@"我是單行字符輸入框";
- //字體顏色
- textfield001.textColor=[UIColoryellowColor];
- //字體對方方式
- textfield001.textAlignment=NSTextAlignmentRight;
- //光鼠標顏色
- textfield001.tintColor=[UIColorgreenColor];
- //字體大小
- textfield001.font=[UIFontsystemFontOfSize:10.0];
- //其他屬性
- //左間距視圖及模式
- UIImageView*leftImage=[[UIImageViewalloc]initWithFrame:CGRectMake(0.0,0.0,30.0,30.0)];
- leftImage.contentMode=UIViewContentModeScaleAspectFit;
- leftImage.image=[UIImageimageNamed:@"leftImage"];
- textfield001.leftView=leftImage;
- textfield001.leftViewMode=UITextFieldViewModeAlways;
- //右間距視圖及模式,若再設置清除按鈕,則清除按鈕是無效的,即兩者不能同時設置
- //UIImageView*rightImage=[[UIImageViewalloc]initWithFrame:CGRectMake(0.0,0.0,30.0,30.0)];
- //rightImage.contentMode=UIViewContentModeScaleAspectFit;
- //rightImage.image=[UIImageimageNamed:@"rightImage"];
- //textfield001.rightView=rightImage;
- //textfield001.rightViewMode=UITextFieldViewModeAlways;
- //清除按鈕,如果設置了右間距視圖,則無效,即兩者不能同時設置
- textfield001.clearButtonMode=UITextFieldViewModeWhileEditing;
- //鍵盤類型
- textfield001.keyboardType=UIKeyboardTypeDefault;
- //鍵盤中回車鍵類型
- textfield001.returnKeyType=UIReturnKeySend;
- //鍵盤中回車鍵有輸入時,回車鍵才可點擊,默認是可點擊,即NO
- textfield001.enablesReturnKeyAutomatically=NO;
- //輸入框字符明文,或密文方式,默認是明文,即NO
- textfield001.secureTextEntry=NO;
- //設置代理
- /*
- 1代理通常設置其他需要實現textfile代理方法的對象
- 2要設置協議
- 3實現協議方法
- */
- textfield001.delegate=self;
-
- @interfaceViewController()
-
- @end
- //輸入源視圖,默認是鍵盤,可通過設置輸入源視圖是其他控件,或自定義控件
- //textfield001.inputView=[[UIViewalloc]initWithFrame:CGRectMake(0.0,0.0,320.0,300.0)];
- //輸入源視圖中的頂端視圖,默認是沒有的
- UIButton*button=[UIButtonbuttonWithType:UIButtonTypeCustom];
- button.backgroundColor=[UIColorcolorWithWhite:0.0alpha:0.3];
- button.frame=CGRectMake(0.0,0.0,CGRectGetWidth(self.view.bounds),40.0);
- [buttonsetTitle:@"隱藏鍵盤"forState:UIControlStateNormal];
- [buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];
- [buttonsetTitleColor:[UIColoryellowColor]forState:UIControlStateHighlighted];
- [buttonaddTarget:selfaction:@selector(hiddenKeyboard)forControlEvents:UIControlEventTouchUpInside];
- textfield001.inputAccessoryView=button;
- //UITextFieldDelegate
-
- //即將開始編輯
- -(BOOL)textFieldShouldBeginEditing:(UITextField*)textField
- {
- NSLog(@"即將開始編輯");
- returnYES;
- }
-
- //已經開始編輯
- -(void)textFieldDidBeginEditing:(UITextField*)textField
- {
- NSLog(@"已經開始編輯");
- }
-
- //即將結束編輯
- -(BOOL)textFieldShouldEndEditing:(UITextField*)textField
- {
- NSLog(@"即將結束編輯");
- returnYES;
- }
-
- //已經結束編輯
- -(void)textFieldDidEndEditing:(UITextField*)textField
- {
- NSLog(@"已經結束編輯");
- }
-
- //正在編輯
- -(BOOL)textField:(UITextField*)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(NSString*)string
- {
- NSLog(@"正在編輯=%@",textField.text);
-
- /*
- 1常用來獲取當前輸入的信息
- 2用來判斷當前的輸入是否限制的字符,如限制只能輸入大小寫字母,或只能輸入數字等
- 3用來判斷輸入的字符長度限制
- */
-
-
- returnYES;
- }
-
- //應該清除
- -(BOOL)textFieldShouldClear:(UITextField*)textField
- {
- NSLog(@"點擊了清除鍵");
-
- returnYES;
- }
-
- //應該回車
- -(BOOL)textFieldShouldReturn:(UITextField*)textField
- {
- NSLog(@"點擊了回車鍵");
-
- //隱藏鍵盤
- //方法1失去第一響應
- //[textFieldresignFirstResponder];
- //方法2結束編輯
- [textFieldendEditing:YES];
-
- returnYES;
- }
- -(void)hiddenKeyboard
- {
- //當前視圖結束編輯
- [self.viewendEditing:YES];
- }