TextField(实训)

来源:互联网 发布:int java 编辑:程序博客网 时间:2024/06/05 15:50

一直写网络请求,忘了说登录注册界面了。其实主要就是一些按钮,但是感觉这里很多小细节,比如button的圆角啊什么的还是很有用处的,一般app都用得到。不多说,贴代码!!!这里只贴登录界面,注册界面用到的在登录注册里面都有了。

import UIKitclass SignViewController: UIViewController, UITextFieldDelegate {    var phoneText : UITextField!    var passwordText : UITextField!            override func viewDidLoad() {        super.viewDidLoad()        self.navigationItem.title = "登录"        self.view.backgroundColor = UIColor(patternImage: UIImage(named: "sign_bg")!)        self.view.contentMode = .scaleAspectFill                phoneText = UITextField(frame: CGRect(x: 50, y: 240, width: screenwidth-100, height: 40))        phoneText.attributedPlaceholder=NSAttributedString(string: "手机号", attributes: [NSForegroundColorAttributeName: kRGBColorFromHex(rgbValue: 0x656565)])        phoneText.textColor = kRGBColorFromHex(rgbValue: 0xffffff)        phoneText.layer.borderWidth=1        phoneText.layer.borderColor=kRGBColorFromHex(rgbValue: 0x656565).cgColor        phoneText.layer.cornerRadius=10        phoneText.clearButtonMode=UITextFieldViewMode.whileEditing;        phoneText.keyboardType = UIKeyboardType.numberPad        let phoneLeftView = UIView(frame: CGRect(x:10, y:10, width:40, height:40));        let phonePic = UIImageView(frame: CGRect(x:10,y:10, width:20, height:20));        phonePic.contentMode = .scaleAspectFill        phonePic.image=UIImage(named: "sign_man.png");        phoneLeftView.addSubview(phonePic)        phoneText.leftView = phoneLeftView        phoneText.leftViewMode=UITextFieldViewMode.always;                passwordText = UITextField(frame: CGRect(x: 50, y: 300, width: screenwidth-100, height: 40))        //passwordText.placeholder = "密码"        passwordText.attributedPlaceholder=NSAttributedString(string: "密码", attributes: [NSForegroundColorAttributeName: kRGBColorFromHex(rgbValue: 0x656565)])        passwordText.textColor = kRGBColorFromHex(rgbValue: 0xffffff)        passwordText.layer.borderWidth=1        passwordText.layer.borderColor=kRGBColorFromHex(rgbValue: 0x656565).cgColor        passwordText.layer.cornerRadius=10        passwordText.clearButtonMode=UITextFieldViewMode.whileEditing;        let passwordLeftView = UIView(frame: CGRect(x:0, y:0, width:40, height:40));        let passwordPic = UIImageView(frame: CGRect(x:10,y:10, width:20, height:20));        passwordPic.contentMode = .scaleAspectFill        passwordPic.image=UIImage(named: "sign_lock.png");        passwordLeftView.addSubview(passwordPic)        passwordText.leftView = passwordLeftView        passwordText.leftViewMode=UITextFieldViewMode.always;                self.view.addSubview(phoneText)        self.view.addSubview(passwordText)    }        override func didReceiveMemoryWarning() {        super.didReceiveMemoryWarning()        // Dispose of any resources that can be recreated.    }            func kRGBColorFromHex(rgbValue: Int) -> (UIColor) {        return UIColor(red: ((CGFloat)((rgbValue & 0xFF0000) >> 16)) / 255.0,green: ((CGFloat)((rgbValue & 0xFF00) >> 8)) / 255.0,blue: ((CGFloat)(rgbValue & 0xFF)) / 255.0,alpha: 1.0)    }}

看看这里都用到了什么来设置我们平时用到的各种常用的控件。最基本的背景颜色,虽然是backgroundcolor,但是不只是能设置背景颜色,还可以用来设置背景图片。

如果想要设置在未输入的时候的提示文字,如果是想只设置文字,则直接用placeholder就好,这里由于背景的原因,还要改变一下颜色,所以就用了上面的方法,内容和颜色可以一次性搞定。

然后就是边框的宽度,颜色还有圆角的角度。最后!!!感觉加了这个瞬间app就高大上了一丢丢,就是在输入手机号、密码的框前面可以加一个小图标。这个叫做leftView。我们做好一个想要的大小的UIView,直接加到leftView里就行了。


原创粉丝点击