更改自定义文本字段背景颜色和文本颜色IOS Swift [英] Change custom text field background color and text color IOS Swift

查看:600
本文介绍了更改自定义文本字段背景颜色和文本颜色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
    }

}


推荐答案

将self添加为目标根据文档

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

你有EditingDidBegin等控制事件。

There you have control events for EditingDidBegin and such.

这样的事情:

self.addTarget(self, action: "myFunc", forControlEvents: UIControlEvents.EditingDidBegin);

这篇关于更改自定义文本字段背景颜色和文本颜色IOS Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆