Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change custom text field background color and text color IOS Swift

I'm using following custom text field class to change appearance of the text field. Now I need to change text field's background color, text color and place holder color, when user start editing and end editing the text field. How to do it, using this class.

import Foundation
import UIKit

class CustomTextField: UITextField{

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)

        //Border
        self.layer.cornerRadius = 15.0;
        self.layer.borderWidth = 1.5
        self.layer.borderColor = UIColor.whiteColor().CGColor

        //Background
        self.backgroundColor = UIColor(white: 1, alpha: 0.0)

        //Text
        self.textColor = UIColor.whiteColor()
        self.textAlignment = NSTextAlignment.Center
    }

}
like image 448
Arvin Jayanake Avatar asked Apr 27 '15 06:04

Arvin Jayanake


1 Answers

Add self as a target for the UIControl events you need according to documentation

There you have control events for EditingDidBegin and such.

Something like this:

self.addTarget(self, action: "myFunc", forControlEvents: UIControlEvents.EditingDidBegin);
like image 180
Yaser Avatar answered Sep 19 '22 05:09

Yaser