UITextField控件是開發中,使用頻率比較高的控件了,那麼有必要總結一下。
UITextField *txtAccount = [[UITextField alloc] initWithFrame:CGRectMake(10, 10,300, 30)];
// 設置委託
[txtAccount setDelegate:self];
// 設置佔位符
[txtAccount setPlaceholder:@"賬號"];
// 設置文本對齊
[txtAccount setTextAlignment:NSTextAlignmentLeft];
// 設置樣式
[txtAccount setBorderStyle:UITextBorderStyleRoundedRect];
// 加入view中
[self.view addSubview: txtAccount];
[txtAccount release];
// 設置輸入框,是否可以被修改
// NO-將無法修改,不出現鍵盤
// YES-可以修改,默認值
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
// 當點擊鍵盤的返回鍵(右下角)時,執行該方法。
// 一般用來隱藏鍵盤
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
if (txtAccount == textField) {
[txtAccount resignFirstResponder];
}
return YES;
}
// 當輸入框獲得焦點時,執行該方法。
- (void)textFieldDidBeginEditing:(UITextField *)textField{
NSLog(@"textFieldDidBeginEditing");
}
// 指定是否允許文本字段結束編輯,允許的話,文本字段會失去first responder
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
}
// 指明是否允許根據用戶請求清除內容
- (BOOL)textFieldShouldClear:(UITextField *)textField{
NSLog(@"textFieldDidEndEditing");
return YES;
}
// 文本框的文本,是否能被修改
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}