iOS-個人整理04 - UITextField文本輸入框

UITextField--文本框

UITextField是控制文本輸入和顯示的控件,只能輸入單行


#pragma mark UITextField文本框,單行  
      
    //初始化  
    UITextField *myTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 350, 150, 20)];  
  
    //占位字符,沒有輸入時顯示的文本,點擊文本框時自動清空  
    myTextField.placeholder = @"請輸入手機號";  
  
    //設置鍵盤樣式,數字鍵盤,字母鍵盤等  
    myTextField.keyboardType = UIKeyboardTypeNumberPad;  
      
    //設置邊框樣式  
    myTextField.borderStyle = UITextBorderStyleRoundedRect;  
  
    //設置密碼樣式,使輸入內容隱藏  
    myTextField.secureTextEntry = NO;  
    [self.window addSubview:Line];  
      
    //設置右側小叉號,點擊可以刪去所有輸入內容  
    myTextField.clearButtonMode = UITextFieldViewModeAlways;  
    //設置首字母大寫  
    myTextField.autocapitalizationType = UITextAutocapitalizationTypeWords;  
      
    //通過屬性控制是否可以編輯  
    myTextField.enabled = YES;  
  
    //自定義鍵盤  
    UIView *keyBoardView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.window.frame), 256)];  
     
    //定義一個button放在鍵盤上  
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeSystem];  
    [firstButton setTitle:@"1" forState:UIControlStateNormal];  
    firstButton.frame = CGRectMake(10, 10, 50, 50);  
    [keyBoardView addSubview:firstButton];  
    firstButton.backgroundColor = [UIColor whiteColor];  
    //設置點擊效果  
    [firstButton addTarget:self action:@selector(keyButton:) forControlEvents:UIControlEventTouchUpInside];  
      
    myTextField.inputView = keyBoardView;  
      
    //顯示  
    [self.window addSubview:myTextField];  
      
    //新建一個輸入框,設置代理方法  
    UITextField *delegateTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 200, 100, 100)];  
    delegateTextField.borderStyle = UITextBorderStyleLine;  
    delegateTextField.placeholder = @"代理方法";  
      
    delegateTextField.keyboardType = UIKeyboardTypeDefault;  
    //設置代理方法,代理人是本類  
    delegateTextField.delegate = self;  
    [self.window addSubview:delegateTextField];  
  
    return YES;  
}  
  
//自定義按鈕的  
-(void)keyButton:(UIButton*)sender  
{  
    sender.selected = !sender.selected;  
    UITextField *field = (UITextField*)[self.window viewWithTag:1001];  
    NSString *title = [sender titleForState:UIControlStateNormal];  
    field.text = [field.text stringByAppendingString:title];  
  
    //回收鍵盤 取消第一響應者 結束編輯狀態  
    [field resignFirstResponder];  
  
    //另一種方法回收鍵盤  
    //[self.view endEditing YES];  
  
    //變為第一響應者  
    [field becomeFirstResponder];  
 }  

遵守代理協議<TextFieldDelegate>后可執行下面的方法

#pragma makr 代理方法  
  
//是否可以開始編輯狀態  
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField  
{  
    NSLog(@"%s",__func__);  
    return YES;  
}  
  
//已經進入編輯狀態  
-(void)textFieldDidBeginEditing:(UITextField *)textField  
{  
    NSLog(@"%s",__func__);  
}  
//是否可以結束編輯  
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField  
{  
    NSLog(@"%s",__func__);  
    return YES;  
}  
//已經結束編輯狀態  
-(void)textFieldDidEndEditing:(UITextField *)textField  
{  
    NSLog(@"%s",__func__);  
}  
//點擊右下角return按鈕所觸發的代理方法  
-(BOOL)textFieldShouldReturn:(UITextField *)textField  
{  
    NSLog(@"return");  
    //在此回收鍵盤  
    [textField resignFirstResponder];  
    return YES;  
}  
  
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  
{  
    //可以限制文字的改變  
    return YES;  
}   

設置了代理方法后,UITextField的各種狀態會觸發代理方法,在本例中代理人設置成了self,也就是本類,然后在本類中實習代理協議要求的方法,這些方法都是@optional的,可以選擇性實現

長按文字提示粘貼復制默認是英文,改為中文:
在info.plist中添加Localized resources can be mixed,設置為YES.

運行,就OK了

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容