Tuesday, September 15, 2015

UITextField - Create a textfield programmatically

Update - July 12, 2017 -  Xcode 8.3.3 & Swift 3.1
Update - October 13, 2015
1. Change the background color for various conditions.
2. Hide the keyboard while touching outside the text field.

Edit the ViewController.swift file as:


Update - July 12, 2017 -  Xcode 8.3.3 & Swift 3.1

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {
    
    var textField : UITextField!
    var label : UILabel!
    let str : String = "You have entered: "
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //Color #1 - Initial color
        view.backgroundColor = UIColor.yellow
        
        let placeholder = NSAttributedString(string: "Enter here", attributes: [NSForegroundColorAttributeName: UIColor.lightGray])
        
        textField = UITextField(frame: CGRect(x: 50, y: 100, width: 200, height: 20))
        
        textField.attributedPlaceholder = placeholder
        textField.textColor = UIColor.black
        textField.delegate = self
        textField.borderStyle = UITextBorderStyle.roundedRect
        textField.clearsOnBeginEditing = true
        view.addSubview(textField)
        
        label = UILabel(frame: CGRect(x: 50, y: 200, width: 200, height: 20))
        label.text = str
        view.addSubview(label)
    }
    
    func textFieldDidBeginEditing(_ textField: UITextField) {
        //Color #2 - While selecting the text field
        view.backgroundColor = UIColor.purple
    }
    
    func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        
        //Color #3 - While touching outside the textField.
        view.backgroundColor = UIColor.cyan
        
        //Hide the keyboard
        textField.resignFirstResponder()
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        //Display the result.
        label.text = str+textField.text!
        
        //Color #4 - After pressing the return button
        view.backgroundColor = UIColor.orange
        textField.resignFirstResponder() //Hide the keyboard
        return true
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

Update - October 13, 2015

import UIKit

class ViewController: UIViewController, UITextFieldDelegate {

    var textField : UITextField!
    var label : UILabel!
    let str : String = "You have entered:"
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //Color #1 - Initial color
        view.backgroundColor = UIColor.yellowColor()
        
        let placeholder = NSAttributedString(string: "Enter here", attributes: [NSForegroundColorAttributeName: UIColor.lightGrayColor()])
        
        textField = UITextField(frame: CGRectMake(50, 100, 200, 20))

        textField.attributedPlaceholder = placeholder
        textField.textColor = UIColor.blackColor()
        textField.delegate = self
        textField.borderStyle = UITextBorderStyle.RoundedRect
        textField.clearsOnBeginEditing = true
        view.addSubview(textField)
        
        label = UILabel(frame: CGRectMake(50, 200, 200, 20))
        label.text = str
        view.addSubview(label)
    }
    
    func textFieldDidBeginEditing(textField: UITextField) {
        
        //Color #2 - While selecting the text field
        view.backgroundColorUIColor.purpleColor()
    }
    
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        
        //Color #3 - While touching outside the textField.
        view.backgroundColor = UIColor.cyanColor()
        
        //Hide the keyboard
        textField.resignFirstResponder()
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
        
        //Display the result.
        label.text = str+textField.text
        
        //Color #4 - After pressing the return button
        view.backgroundColorUIColor.orangeColor()
        
        textField.resignFirstResponder() //Hide the keyboard
        return true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

While editing the text field:



The result after hitting the return key:


1 comment: