Change custom text field background color and text color IOS Swift

半腔热情 提交于 2019-11-30 18:27:44

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);

In your CustomTextField class you can add a property observer:

var change: Bool = false {
    didSet {
        textColor = change ? .yellow : .black
        backgroundColor = change ? .blue : .white
    }
}

and in your ViewController:

func textFieldDidBeginEditing(textField: UITextField) {
    customTextField.change = true
}

func textFieldDidEndEditing(textField: UITextField) {
    customTextField.change = false
}

Don't forget to set the delegate of your textfield, in storyboard or programmatically.

EDIT:
Shortened the code and updated for Swift 3

Select the text field -> click "identity inspector" icon -> click on Plus button "user Defined runtime attributes" -> add this text "_placeholderLabel.textColor" in "key path" field -> choose the "Type" "color" and set the value color.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!