在Swift中子类化UITextfield

时间:2018-12-28 04:12:27

标签: ios swift uitextfield

我目前正在我的视图控制器中初始化很多属性,但是感觉非常非常混乱。我一直在创建UITextField,例如:

lazy var passwordTextField: UITextField = {
        let textField = UITextField()
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 30))
        textField.leftView = paddingView
        textField.leftViewMode = .always
        textField.placeholder = "Password"
        textField.isSecureTextEntry = true
        textField.autocorrectionType = .no
        textField.autocapitalizationType = UITextAutocapitalizationType.none
        textField.attributedPlaceholder = NSAttributedString(string: "Password", attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightGray])
        textField.tintColor = UIColor.white
        textField.borderStyle = .none
        textField.layer.borderColor = UIColor.white.cgColor
        textField.layer.borderWidth = 0.7
        textField.delegate = self
        textField.textColor = UIColor.white
        return textField
    }()

每个UIViewController我有大约5个。我想创建一个UITextField类,该类基本上是一个工厂,用于抽取自定义UITextField。我该怎么办?

3 个答案:

答案 0 :(得分:2)

子类化任何项目都非常容易。做类似的事情

class MyTextField: UITextField {
    override init(frame: CGRect) {
        super.init(frame: frame)
        textFieldSetup()
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        textFieldSetup()
    }

    private func textFieldSetup() {
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 30))
        leftView = paddingView
        leftViewMode = .always
        ...
        ...
        //Add the common properties here that you want replicated for every instance
        ...
        ...
        layer.borderColor = UIColor.white.cgColor
        layer.borderWidth = 0.7
        textColor = UIColor.white
    }
}

然后,您可以创建MyTextField而不是UITextField的实例

var passwordTextField: MyTextField!

答案 1 :(得分:1)

您可以像这样对 public function update(Request $request, $id) { $this->validate($request, [ 'key' => 'required|string|max:255', // 'value' => 'required', ]); $product = product::update([ 'key' => $request['key'], 'name' => $request['name'], // 'value' => $request['value'], ]); return redirect()->route('products.show', $product); } 进行子类化

UITextField

要使用自定义标签或文本字段,

class CustomTextField: UITextField {

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        self.customInit()
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        self.customInit()
    }

    func customInit(){
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 30))
        self.leftView = paddingView
        self.leftViewMode = .always
        self.placeholder = "Password"
        self.isSecureTextEntry = true
        self.autocorrectionType = .no
        self.autocapitalizationType = .none
        self.attributedPlaceholder = NSAttributedString(string: "Password", attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightGray])
        self.tintColor = .white
        self.borderStyle = .none
        self.layer.borderColor = UIColor.white.cgColor
        self.layer.borderWidth = 0.7
        self.textColor = .white
    }
}

答案 2 :(得分:1)

您可以为文本字段创建扩展名,

extension UITextField {

    func setLeftPaddingPoints(_ amount:CGFloat){
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
        self.leftView = paddingView
        self.leftViewMode = .always
    }
    func setRightPaddingPoints(_ amount:CGFloat) {
        let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
        self.rightView = paddingView
        self.rightViewMode = .always
    }
}