Tuesday 28 February 2017

Dynamic UITextview with button




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

import UIKit

class ViewController: UIViewController {

    var tv1 : UITextView!
    var tv2 : UITextView!
    
    var btn1 : UIButton!
    var btn2 : UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // create tv
        tv1 = UITextView()
        tv1.frame = CGRect(x: 50, y: 100, width: 300, height: 36)
        tv1.text = "Default Textview 1"
    
        view.addSubview(tv1)
        // create tv2
        tv2 = UITextView()
        tv2.frame = CGRect(x: 50, y: 150, width: 300, height: 36)
        tv2.text = "Default Textview 2"
        
        view.addSubview(tv2)
        
        // creating button for set data
        btn1 = UIButton()
        btn1.frame = CGRect(x: 50.0, y: 200, width: 120, height: 36)
        btn1.backgroundColor = UIColor.darkGrayColor()
        btn1.setTitle("SetData", forState: UIControlState.Normal);
        btn1.tag = 1
        btn1.addTarget(self, action: "getSetData:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn1)
        
        // creating button for get and set data
        btn2 = UIButton()
        btn2.frame = CGRect(x: 50.0, y: 250, width: 120, height: 36)
        btn2.backgroundColor = UIColor.darkGrayColor()
        btn2.tag = 2
        btn2.setTitle("GetData", forState: UIControlState.Normal);
        btn2.addTarget(self, action: "getSetData:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn2)
        
    }

    func getSetData(sender : UIButton)
    {
        var sendTag = sender.tag
        if sendTag == 1
        {  tv1.text = " hello textview, you changed" }
        else if sendTag == 2
        {
            tv2.text = tv1.text
        }
    }

    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


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.
    }


}


Dynamic Button -2 : ButtonType




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

import UIKit

class ViewController: UIViewController {

    var label1 : UILabel!
    var btn1 = UIButton.buttonWithType(UIButtonType.System) as UIButton
     var btn2 = UIButton.buttonWithType(UIButtonType.ContactAdd) as UIButton
    var btn3 = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton
    var btn4 = UIButton.buttonWithType(UIButtonType.InfoDark) as UIButton
    var btn5 = UIButton.buttonWithType(UIButtonType.InfoLight) as UIButton

    
    /*
    - Button - 1 is use here for single click
    - Button - 2 and 3 is for multiple button click same function
    */
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // creating text
        label1 = UILabel()
        label1.frame = CGRect(x: 100, y: 100, width: 500, height: 60)
        label1.text = "Default Text"
        view.addSubview(label1);
        
        // creating button-1
        btn1.sizeToFit()
        btn1.frame = CGRect(x: 150, y: 150, width: 120, height: 40)
        btn1.setTitle("Button 1", forState: UIControlState.Normal)
        btn1.backgroundColor = UIColor.purpleColor()
        view.addSubview(btn1)
        
        // creating button-2
        btn2.sizeToFit()
        btn2.frame = CGRect(x: 150, y: 200, width: 120, height: 40)
        btn2.setTitle("Button 2", forState: UIControlState.Normal)
        btn2.backgroundColor = UIColor.purpleColor()
        view.addSubview(btn2)
        
        // creating button-3
        btn3.sizeToFit()
        btn3.frame = CGRect(x: 150, y: 250, width: 120, height: 40)
        btn3.setTitle("Button 3", forState: UIControlState.Normal)
        btn3.backgroundColor = UIColor.purpleColor()
        view.addSubview(btn3)
        
        // creating button-4
        btn4.sizeToFit()
        btn4.frame = CGRect(x: 150, y: 300, width: 120, height: 40)
        btn4.setTitle("Button 4", forState: UIControlState.Normal)
        btn4.backgroundColor = UIColor.purpleColor()
        view.addSubview(btn4)
        
        // creating button-5
        btn5.sizeToFit()
        btn5.frame = CGRect(x: 150, y: 350, width: 120, height: 40)
        btn5.setTitle("clickto change in label", forState: UIControlState.Normal)
      
        
    }

    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


Dynamic button - 1

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

import UIKit

class ViewController: UIViewController {

    var label1 : UILabel!
    var btn1 : UIButton!
    var btn2 : UIButton!
    var btn3 : UIButton!
    
    /*
    - Button - 1 is use here for single click
    - Button - 2 and 3 is for multiple button click same function
    */
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        // creating text
        label1 = UILabel()
        label1.frame = CGRect(x: 100, y: 100, width: 500, height: 60)
        label1.text = "Default Text"
        view.addSubview(label1);
        
        // creating button-1
        btn1 = UIButton()
        btn1.sizeToFit()
        btn1.frame = CGRect(x: 150, y: 150, width: 120, height: 40)
        btn1.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)
        btn1.layer.borderWidth = 2
        btn1.layer.cornerRadius = 10
        btn1.setTitle("Button 1", forState: UIControlState.Normal)
        // or can use : btn1.setTitle("Button 1", forState: UIControlState.Highlighted)
        btn1.backgroundColor = UIColor.orangeColor()
        btn1.contentHorizontalAlignment = UIControlContentHorizontalAlignment.Right
        btn1.addTarget(self, action: "buttonClicked1:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn1)
        
        // creating button-2
        btn2 = UIButton()
        btn2.sizeToFit()
        btn2.frame = CGRect(x: 150, y: 200, width: 120, height: 40)
        btn2.setTitle("Button 2", forState: UIControlState.Normal)
        // or can use : btn2.setTitle("Button 1", forState: UIControlState.Highlighted)
        btn2.backgroundColor = UIColor.orangeColor()
        btn2.tag = 1
        btn2.addTarget(self, action: "buttonClicked2:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn2)
        
        // creating button-3
        btn3 = UIButton()
        btn3.sizeToFit()
        btn3.frame = CGRect(x: 150, y: 250, width: 120, height: 40)
        btn3.setTitle("Button 3", forState: UIControlState.Normal)
        // or can use : btn2.setTitle("Button 1", forState: UIControlState.Highlighted)
        btn3.backgroundColor = UIColor.orangeColor()
        btn3.tag = 2
        btn3.addTarget(self, action: "buttonClicked2:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(btn3)
        
    }

    func buttonClicked1(sender : UIButton)
    {
        label1.text = " Hello Button Clicked event"
    }
    
    func buttonClicked2(sender : UIButton)
    {
        var sendbtn : UIButton = sender
        if sendbtn.tag == 1
        { label1.text = " tab button 1.1 " }
        else if sendbtn.tag == 2
        { label1.text = " tag button 1.2 " }
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}