Swift custom control-input box and swift control input box

Source: Internet
Author: User

Swift custom control-input box and swift control input box

When developing Android projects, common functions are often encapsulated into controls. One is to reduce the amount of code, the other looks concise, and Apple development also prefers to encapsulate them into controls, then drag and drop in the storyboard. for IOS development, the code quality is not very high because it belongs to the cainiao level. If there is a problem, please note that one record here is to forget the other and hope to communicate more.

This is a custom input box, which is often used in projects. The most common example is textfield on the left side of the label, as shown below:


The Code is as follows:

//// TextFieldValidator. swift // CustomWidget /// Created by System Administrator on 15/3/21. // Copyright (c) 2015 Zhangjie. 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 () encode = 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 encode (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: delimiter) {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 Zhangjie. all rights reserved. // import UIKitclass 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 ("userName", placeholder: "enter userName") userName. setReturnKey (UIReturnKeyType. next) userPasswd. initWithFrame ("password", placeholder: "enter your password") userPasswd. setSecureTextEntry (true) userPasswd. setReturnKey (UIReturnKeyType. done)} override func didReceiveMemoryWarning () {super. didReceiveMemoryWarning () // Dispose of any resources that can be recreated .}}

Note:

How do I add a custom control to the storyboard?

Because the custom control is not a system control, you can drag it directly in Xcode. First, you need to drag the custom parent class. For example, UIView is inherited here, therefore, drag a UIView from Xcode to storyboard, and set the class of UIView to the custom control name.


View shortcuts for all methods in the class?

In the blank area, click esc to display all the methods for this class.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.