//设置全局变量,将下面的替换即可
//var myTextField = UITextField();
//系统生成的viewDidLoad()方法
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//创建局部变量UITextField
let myTextField = UITextField.init(frame: CGRectMake(20, 100, self.view.frame.width-40, 60));
//设置文本
//myTextField.text = "这是一个输入框";
//设置占位符
myTextField.placeholder = "请输入你的内容......";
//设置字体大小
myTextField.font = UIFont.systemFontOfSize(22);
//设置编辑框右侧出现的删除图标
myTextField.clearButtonMode = UITextFieldViewMode.WhileEditing;
//设置边框大小,默认为0
myTextField.layer.borderWidth = 2;
//设置边框颜色,默认为黑色
myTextField.layer.borderColor = UIColor.redColor().CGColor;
//设置圆角
myTextField.layer.cornerRadius = 8;
//设置背景颜色
myTextField.backgroundColor = UIColor.cyanColor();
//设置是否安全显示
//myTextField.secureTextEntry = true;
//设置键盘样式
//myTextField.keyboardType = UIKeyboardType.NumberPad;//纯数字类型
//设置return键类型
/*typedef enum {
UIReturnKeyDefault, 默认 灰色按钮,标有Return
UIReturnKeyGo, 标有Go的蓝色按钮
UIReturnKeyGoogle,标有Google的蓝色按钮,用语搜索
UIReturnKeyJoin,标有Join的蓝色按钮
UIReturnKeyNext,标有Next的蓝色按钮
UIReturnKeyRoute,标有Route的蓝色按钮
UIReturnKeySearch,标有Search的蓝色按钮
UIReturnKeySend,标有Send的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyYahoo,标有Yahoo的蓝色按钮
UIReturnKeyEmergencyCall, 紧急呼叫按钮
} UIReturnKeyType;*/
myTextField.returnKeyType = UIReturnKeyType.Search;
//添加左视图或右视图
let leftImgView = UIImageView.init(frame: CGRectMake(0, 0, 60, 60));
leftImgView.image = UIImage(named: "login_pwd.png");
myTextField.leftView = leftImgView;
//设置视图显示的方式,添加视图后必须写
myTextField.leftViewMode = UITextFieldViewMode.Always;
//设置代理
myTextField.delegate = self;
//添加视图
self.view.addSubview(myTextField);
}
//UITextFieldDelegate
func textFieldDidBeginEditing(textField: UITextField) {
print("执行了开始编辑方法。。。。");
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
print("执行了编辑框内容被某个值替换了的方法",string);
return true;
}