//1.初始化UITextField
let userName=UITextField(frame: CGRectMake(0, 100, 100, 100));
//2.将文本框userName添加到当前视图中
self.view.addSubview(userName);
//3.文本框默认显示文字
userName.placeholder="请输入手机号";
//4.设置字体大小
userName.font=UIFont.systemFontOfSize(16);
//5.当编辑时文本框右侧出现删除小图标,登陆界面经常会用到
userName.clearButtonMode=UITextFieldViewMode.WhileEditing;
//6.如果需要设置文本框边框颜色,需要同时设置颜色及边框大小,否则会没有边框效果
userName.layer.borderColor=UIColor.blackColor();
userName.layer.borderWidth=1;
//7.设置文本框圆角
userName.layer.cornerRadius=5;
//8.有时候文本框左视图需要添加图片
//8.1添加用户名左视图
let NameLeftView:UIView=UIView(frame: CGRectMake(0, 0, 40, 60));
let namePic:UIImageView=UIImageView(frame: CGRectMake(5,20, 20, 20));
namePic.image=UIImage(named: "iconfont-phone.png");
NameLeftView.addSubview(namePic);
userName.leftView=NameLeftView;
//下面这句代码是指图片何时显示
userName.leftViewMode=UITextFieldViewMode.Always;
//9.文本框内字符以* 显示
userName.secureTextEntry=true;
//10.边框圆角
userName.borderStyle = UITextBorderStyleRoundedRect;
新闻热点
疑难解答