UITextField *txtAccount = [[UITextField alloc] initWithFrame:CGRectMake(10, 10,300, 30)]; 字體
// 設置委託 spa
[txtAccount setDelegate:self]; orm
// 設置佔位符 string
[txtAccount setPlaceholder:@"帳號"]; it
// 設置顏色 請求
[txtAccount setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; 方法
// 設置字體 樣式
[txtAccount setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"]; di
// 設置文本對齊 鍵盤
[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;
}