In the development of the Android project often used to encapsulate the functionality of common functions to use, one is to reduce the amount of code, the other looks concise, and for Apple development also like to encapsulate into a control, and then storyboard inside the drag, for iOS development itself belongs to rookie level, so the code quality is not very high, If there is a problem, please point out that there is a memo and another hope for a lot of communication.
This is a custom input box, the input box is often used in the project, the most common such as the left is the label to the right is TextField, as follows:
The code is as follows
textfieldvalidator.swift//customwidget////Created by System Administrator on 15/3/21.//Copyright (c) 2015 JWZ Hangjie. All rights Reserved.//import Uikitclass 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 (Labeltex T) 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: "", Placeholde R: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) {Txtfi Eldflag.text = text} Func getText ()->nsstring{return txtfieldflag.text; } func settxtflagalignment (option:nstextalignment) {txtflag.textalignment = option} func Setspaci Ng (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 Setclearbutto Nmode (mode:uitextfieldviewmode) {Txtfieldflag.clearbuttonmode = mode} func Setreturnkey (Type:uireturnkey Type) {Txtfieldflag.returnkeytype = type}}
////ViewController.s wift//customwidget////Created by System Administrator on 15/3/21.//Copyright (c) 2015 Jwzhangjie. All rights Reserved.//import Uikitclass Viewcontroller:uiviewcontroller {@IBOutlet weak var Username:textfieldvalida Tor! @IBOutlet weak var userpasswd:textfieldvalidator! Override Func Viewdidload () {super.viewdidload ()//Do any additional setup after loading the view, typical Ly from a nib. Username.initwithframe ("username", placeholder: "Please enter user name") username.setreturnkey (Uireturnkeytype.next) userpasswd. initWithFrame ("Password", placeholder: "Please enter user password") userpasswd.setsecuretextentry (True) Userpasswd.setreturnkey (uire Turnkeytype.done)} override func didreceivememorywarning () {super.didreceivememorywarning () Dispose of any resources the can be recreated. }}
Note the point:
How do I add a custom control to storyboard?
Since the custom control is not a system control, it is possible to drag directly in Xcode, first of all to drag and drop the custom parent class, for example, UIView is inherited here, so drag a uiview from Xcode to storyboard, Then set the class of UIView to the name of the custom control
View the shortcut keys for all methods in a class?
In the blank space, click ESC to show all the methods of the class
Swift custom Control--Input box