标签:
在开发安卓项目的时候经常对于常用的功能进行封装成控件来使用,一个是减少代码量,另一个看起来简洁,而对于苹果开发也喜欢封装成控件,然后再storyboard里面拖拽,对于IOS开发自己属于菜鸟级别所以代码质量不是很高,如果那里有问题,请指出,这里记录一个是备忘另一个希望多多交流。
这篇是自定义输入框,输入框在项目中经常用到,最常见的比如左边是label右边是textfield,效果图如下:
代码如下
// // TextFieldValidator.swift // CustomWidget // // Created by System Administrator on 15/3/21. // Copyright (c) 2015年 jwzhangjie. All rights reserved. // import UIKit class TextFieldValidator : UIView{ var txtFlag:UILabel! var txtFieldFlag:UITextField! var spacing:CGFloat? var delegate:UITextFieldDelegate? required init(coder aDecoder: NSCoder) { super.init(coder: aDecoder) txtFlag = UILabel() txtFieldFlag = UITextField() } func initWithFrame(labelText:NSString, text:NSString, placeholder:NSString, spacing:CGFloat){ var width:CGFloat = frame.size.width; var height:CGFloat = frame.size.height; txtFlag = UILabel(frame: CGRectMake(0, 0, width / 3, height)) txtFlag.backgroundColor = UIColor.clearColor() txtFlag.textAlignment = NSTextAlignment.Left self.spacing = spacing self.addSubview(txtFlag) txtFieldFlag = UITextField(frame: CGRectMake(width / 3 + spacing, 0, width-width/3 + spacing, height)) txtFieldFlag.borderStyle = UITextBorderStyle.RoundedRect txtFieldFlag.clearButtonMode = UITextFieldViewMode.Always self.addSubview(txtFieldFlag) setText(text) setLabelText(labelText) txtFieldFlag.placeholder = placeholder } func initWithFrame(labelText:NSString, placeholder:NSString, spacing:CGFloat){ return initWithFrame(labelText, text: "", placeholder: placeholder, spacing: spacing) } func initWithFrame(labelText:NSString, placeholder:NSString){ return initWithFrame(labelText, text: "", placeholder: placeholder, spacing: 0) } func setDelegate(delegate:UITextFieldDelegate){ self.delegate = delegate txtFieldFlag.delegate = delegate } func setLeftTextFlag(text:NSString){ txtFlag.text = text } func setLabelText(text:NSString){ txtFlag.text = text } func setText(text:NSString){ txtFieldFlag.text = text } func getText()->NSString{ return txtFieldFlag.text; } func setTxtFlagAlignment(option:NSTextAlignment){ txtFlag.textAlignment = option } func setSpacing(spacing:CGFloat){ self.spacing = spacing; txtFieldFlag.frame=CGRectMake(txtFlag.frame.size.width+spacing, 0, self.frame.size.width-txtFlag.frame.size.width-spacing, self.frame.size.height); } func setSecureTextEntry(option:Bool){ txtFieldFlag.secureTextEntry = option } func setClearButtonMode(mode:UITextFieldViewMode){ txtFieldFlag.clearButtonMode = mode } func setReturnKey(type:UIReturnKeyType){ txtFieldFlag.returnKeyType = type } }
// // ViewController.swift // CustomWidget // // Created by System Administrator on 15/3/21. // Copyright (c) 2015年 jwzhangjie. All rights reserved. // import UIKit class ViewController: UIViewController { @IBOutlet weak var userName: TextFieldValidator! @IBOutlet weak var userPasswd: TextFieldValidator! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. userName.initWithFrame("用户名", placeholder: "请输入用户名") userName.setReturnKey(UIReturnKeyType.Next) userPasswd.initWithFrame("密码", placeholder: "请输入用户密码") userPasswd.setSecureTextEntry(true) userPasswd.setReturnKey(UIReturnKeyType.Done) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } }
如何将自定义控件加入storyboard?
由于自定义控件不是系统控件,所以能直接在Xcode中拖拽,首先需要拖拽自定义的父类,比如这里继承的是UIView,所以从Xcode中拖拽一个UIView到storyboard中,然后将UIView的class设置为自定义控件名称
查看类中的所有方法的快捷键?
在空白处,点击esc会显示出该类的所有方法
标签:
原文地址:http://blog.csdn.net/jwzhangjie/article/details/44523451