- alloc init 創(chuàng)建文本域
- .frame CGRectMake(x,y,w,h) 設(shè)置文本位置
- .text 設(shè)置文字內(nèi)容
- .font 設(shè)置字體 UIFont systemFontOfSize:15
- .textColor 設(shè)置字體顏色
- .borderStyle 設(shè)置邊框風(fēng)格 默認(rèn)圓角邊框UITextBorderStyleRoundedRect
- .keyboardType 設(shè)置虛擬鍵盤風(fēng)格
UIKeyboardTypeDefault默認(rèn)風(fēng)格
UIKeyboardTypeNamePhonePad:字母和數(shù)字組合風(fēng)格
UIKeyboardTypeNumberPad:純數(shù)字風(fēng)格 - .placeholder 設(shè)置占位符,提示文字信息
- .secureTextEntry 是否作為密碼輸入,圓點(diǎn)顯示
- .delegate 設(shè)置代理 <UITextFieldDelegate>
具體使用:
//聲明
@property(strong,nonatomic)UITextField* textField;
//實(shí)現(xiàn)文件中
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_textField = [[UITextField alloc]init];
_textField.frame = CGRectMake(100, 100, 180, 40);
_textField.text = @"user name";
_textField.font = [UIFont systemFontOfSize:15];
_textField.textColor = [UIColor purpleColor];
_textField.borderStyle = UITextBorderStyleRoundedRect;
// _textField.borderStyle = UITextBorderStyleBezel;
//設(shè)置虛擬鍵盤風(fēng)格
//UIKeyboardTypeDefault默認(rèn)風(fēng)格
//UIKeyboardTypeNamePhonePad:字母和數(shù)字組合風(fēng)格
//UIKeyboardTypeNumberPad:純數(shù)字風(fēng)格
_textField.keyboardType = UIKeyboardTypeDefault;
//提示文字信息
//當(dāng)text屬性為空時(shí)癌蚁,顯示此條信息伶氢,淺灰色
_textField.placeholder = @"please input username";
//是否作為密碼輸入伞芹,圓點(diǎn)顯示
_textField.secureTextEntry = YES;
[self.view addSubview:_textField];
_textField.delegate = self;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//使虛擬鍵盤回收
[_textField resignFirstResponder];
}
<UITextFieldDelegate>協(xié)議方法有:
//點(diǎn)擊文本域出現(xiàn)虛擬鍵盤時(shí)調(diào)用
-(void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"編輯開始");
}
//回收虛擬鍵盤薪贫,編輯結(jié)束時(shí)調(diào)用
-(void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"編輯結(jié)束");
}
//是否可以開始輸入忆畅,默認(rèn)YES
//一般用在限制輸入長(zhǎng)度時(shí)
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
//是否可以結(jié)束編輯
//一般用在輸入密碼長(zhǎng)度不夠六剥,不能進(jìn)行下一步
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}