Objective-C代码:
@interface ViewController ()<UITextFieldDelegate>
-(void)setupTextField{
UITextField * testTextField = [[UITextField alloc] initWithFrame:CGRectMake(30, 200, self.view.frame.size.width - 60, 50)];
testTextField.layer.cornerRadius = 6;
testTextField.layer.masksToBounds = YES;
testTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
testTextField.layer.borderWidth = 1;
testTextField.keyboardType = UIKeyboardTypeNumberPad;
testTextField.returnKeyType = UIReturnKeySend;
testTextField.secureTextEntry = YES;
testTextField.delegate = self;
testTextField.placeholder = @"请输入手机号";
[testTextField setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];
testTextField.font = [UIFont systemFontOfSize:16];
testTextField.textColor = [UIColor redColor];
[self.view addSubview:testTextField];
}
-(BOOL)textFieldShouldClear:(UITextField *)textField{
return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
return YES;
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
return YES;
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
}
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField{
return YES;
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
}
-(void)textFieldDidEndEditing:(UITextField *)textField reason:(UITextFieldDidEndEditingReason)reason{
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
return YES;
}
Swift代码:
class ViewController: UIViewController,UITextFieldDelegate
func setupTextField() {
//初始化
let testTextField = UITextField.init(frame: CGRect.init(x: 30, y: 200, width: self.view.frame.size.width - 60, height: 50))
//设置圆角
testTextField.layer.cornerRadius = 6
testTextField.layer.masksToBounds = true
//设置描边
testTextField.layer.borderColor = UIColor.lightGray.cgColor
testTextField.layer.borderWidth = 1
//设置键盘类型
testTextField.keyboardType = UIKeyboardType.numberPad
//设置键盘的回车键
testTextField.returnKeyType = UIReturnKeyType.done
//设置为密文,为NO的话则为明文
testTextField.isSecureTextEntry = true
//设置代理
testTextField.delegate = self
//设置提示语
testTextField.placeholder = "请输入手机号"
//设置提示语的颜色
testTextField.setValue(UIColor.green, forKeyPath: "_placeholderLabel.textColor")
//设置字体大小
testTextField.font = UIFont.systemFont(ofSize: 16)
//设置字体颜色
testTextField.textColor = UIColor.red
//添加textField
self.view.addSubview(testTextField)
}
func textFieldShouldClear(_ textField: UITextField) -> Bool {
//点击清除按钮时调用
return true
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
//点击回车键时调用
return true
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
//将要编辑时调用
return true
}
func textFieldDidBeginEditing(_ textField: UITextField) {
//开始编辑时调用
}
func textFieldShouldEndEditing(_ textField: UITextField) -> Bool {
//将要结束编辑时调用
return true
}
func textFieldDidEndEditing(_ textField: UITextField) {
//结束编辑时调用
}
func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
// if implemented, called in place of textFieldDidEndEditing:
}
func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
//textField内容发生改变时调用
return true
}