UITextField--文本框
UITextField是控制文本輸入和顯示的控件违寿,只能輸入單行
#pragma mark UITextField文本框,單行
//初始化
UITextField *myTextField = [[UITextField alloc]initWithFrame:CGRectMake(100, 350, 150, 20)];
//占位字符,沒有輸入時顯示的文本笙以,點擊文本框時自動清空
myTextField.placeholder = @"請輸入手機號";
//設(shè)置鍵盤樣式,數(shù)字鍵盤,字母鍵盤等
myTextField.keyboardType = UIKeyboardTypeNumberPad;
//設(shè)置邊框樣式
myTextField.borderStyle = UITextBorderStyleRoundedRect;
//設(shè)置密碼樣式,使輸入內(nèi)容隱藏
myTextField.secureTextEntry = NO;
[self.window addSubview:Line];
//設(shè)置右側(cè)小叉號,點擊可以刪去所有輸入內(nèi)容
myTextField.clearButtonMode = UITextFieldViewModeAlways;
//設(shè)置首字母大寫
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];
//設(shè)置點擊效果
[firstButton addTarget:self action:@selector(keyButton:) forControlEvents:UIControlEventTouchUpInside];
myTextField.inputView = keyBoardView;
//顯示
[self.window addSubview:myTextField];
//新建一個輸入框笼吟,設(shè)置代理方法
UITextField *delegateTextField = [[UITextField alloc]initWithFrame:CGRectMake(10, 200, 100, 100)];
delegateTextField.borderStyle = UITextBorderStyleLine;
delegateTextField.placeholder = @"代理方法";
delegateTextField.keyboardType = UIKeyboardTypeDefault;
//設(shè)置代理方法,代理人是本類
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];
//回收鍵盤 取消第一響應(yīng)者 結(jié)束編輯狀態(tài)
[field resignFirstResponder];
//另一種方法回收鍵盤
//[self.view endEditing YES];
//變?yōu)榈谝豁憫?yīng)者
[field becomeFirstResponder];
}
遵守代理協(xié)議<TextFieldDelegate>后可執(zhí)行下面的方法
#pragma makr 代理方法
//是否可以開始編輯狀態(tài)
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
NSLog(@"%s",__func__);
return YES;
}
//已經(jīng)進入編輯狀態(tài)
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
NSLog(@"%s",__func__);
}
//是否可以結(jié)束編輯
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
NSLog(@"%s",__func__);
return YES;
}
//已經(jīng)結(jié)束編輯狀態(tài)
-(void)textFieldDidEndEditing:(UITextField *)textField
{
NSLog(@"%s",__func__);
}
//點擊右下角return按鈕所觸發(fā)的代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSLog(@"return");
//在此回收鍵盤
[textField resignFirstResponder];
return YES;
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
//可以限制文字的改變
return YES;
}
設(shè)置了代理方法后霸旗,UITextField的各種狀態(tài)會觸發(fā)代理方法,在本例中代理人設(shè)置成了self戚揭,也就是本類诱告,然后在本類中實習(xí)代理協(xié)議要求的方法,這些方法都是@optional的民晒,可以選擇性實現(xiàn)
長按文字提示粘貼復(fù)制默認是英文精居,改為中文:
在info.plist中添加Localized resources can be mixed,設(shè)置為YES.
運行,就OK了