Tuesday 28 February 2017

Dynamic TextField



//
//  ViewController.swift
//  TextField
//
//  Created by Divakar Singh on 2/28/17.
//  Copyright (c) 2017 Divakar Singh. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    var tf1 : UITextField!
    var tf2 : UITextField!
    var btn1 : UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        //create button
        btn1 = UIButton()
        btn1.frame = CGRect(x: 50, y: 300, width: 300, height: 30)
        btn1.setTitle("change text from tf1 to tf2", forState: UIControlState.Normal)
        btn1.backgroundColor = UIColor.grayColor()
        btn1.addTarget(self, action: "setData:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn1)
        
        
    // create textfield
        tf1 = UITextField()
        tf1.frame = CGRect(x: 50, y: 100, width: 300, height: 40)
        tf1.text = "type tf1 here"
        tf1.textColor = UIColor.greenColor()
        tf1.backgroundColor = UIColor.grayColor()
        tf1.textAlignment = NSTextAlignment.Left
        //label1.textAlignment = .Right
        tf1.font = UIFont(name: "verdana", size: 16)
        // or shorter way
        //label1.font = UIFont.systemFontOfSize(20)
        
        tf1.layer.shadowOffset = CGSize(width: 3, height: 3)
        tf1.layer.shadowOpacity = 0.7
        tf1.layer.shadowRadius = 2
        
        view.addSubview(tf1)
        
        
        tf2 = UITextField()
        tf2.frame = CGRect(x: 50, y: 150, width: 300, height: 50)
        tf2.text = "type second tf2 here"
        tf2.textColor = UIColor(red: 88.0/255.0, green: 152.0/255.0, blue: 222.0/255.0, alpha: 1)
        tf2.font = UIFont.boldSystemFontOfSize(12)
        tf2.autocapitalizationType = UITextAutocapitalizationType.AllCharacters
        view.addSubview(tf2)
        
    }

    func setData(sender : UIButton)
    {
        tf2.text = tf1.text
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


No comments:

Post a Comment