Подкласс UITextfield в Swift - PullRequest
       0

Подкласс UITextfield в Swift

0 голосов
/ 28 декабря 2018

В настоящее время я инициализирую довольно много свойств в моем контроллере представления, но это выглядит очень, очень грязно.Я создавал 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
    }()

У меня есть около 5 из них на UIViewController.Я хотел бы создать класс UITextField, который по сути является фабрикой, которая выкачивает пользовательские поля UITextFields.Как мне это сделать?

Ответы [ 3 ]

0 голосов
/ 28 декабря 2018

Вы можете создать подкласс 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
    }
}

Для использования пользовательской метки или текстового поля,

class ViewController: UIViewController {

    @IBOutlet weak var myCustomTextField: CustomTextField!
}
0 голосов
/ 28 декабря 2018

Вы можете создать расширение для текстового поля, например,

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
    }
}
0 голосов
/ 28 декабря 2018

Субклассирование любого элемента довольно просто.Просто сделайте что-то вроде

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!
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...