Ios 使用自定义样式在Swift中创建UITextField扩展

Ios 使用自定义样式在Swift中创建UITextField扩展,ios,swift,xcode,swift2,Ios,Swift,Xcode,Swift2,我在视图上有几个UITextField,它们必须看起来相同。我想我可能会创建一个扩展,它将预先设置我的文本字段的样式,我希望这些字段具有相同的样式 let passTextField: UITextField = { let tf = UITextField() //tf.backgroundColor = UIColor.blueColor() tf.translatesAutoresizingMaskIntoConstraints = false tf.lay

我在视图上有几个UITextField,它们必须看起来相同。我想我可能会创建一个扩展,它将预先设置我的文本字段的样式,我希望这些字段具有相同的样式

let passTextField: UITextField = {
    let tf = UITextField()
    //tf.backgroundColor = UIColor.blueColor()
    tf.translatesAutoresizingMaskIntoConstraints = false
    tf.layer.cornerRadius = 25
    tf.layer.borderColor = UIColor(r: 34, g: 140, b: 204, a: 1).CGColor
    tf.layer.borderWidth = 2.0
    tf.layer.masksToBounds = true
    /* Paddings */
    tf.leftView = UIView(frame: CGRectMake(0, 0, 25, 0))
    tf.leftViewMode = UITextFieldViewMode.Always
    tf.rightView = UIView(frame: CGRectMake(0, 0, 25, 0))
    tf.rightViewMode = UITextFieldViewMode.Always
    /* Place Holder Formating */
    let attributes = [
        NSForegroundColorAttributeName: UIColor(r: 34, g: 140, b: 204, a: 1),
        NSFontAttributeName : UIFont(name: "HelveticaNeue-Thin", size: 16)! // Note the !
    ]
    tf.attributedPlaceholder = NSAttributedString(string: "Email", attributes:attributes)


    return tf
}()
因此,这些属性中的大多数应该包含在扩展中,我希望在声明变量时能够向扩展中添加两个属性

你能帮我吗?我一直在寻找如何创建一个扩展,但似乎没有任何结果

谢谢大家!

您可以使用扩展定义来实现,如果方法是新的,您可以使用下面的代码,如果已经存在,则创建一个覆盖方法

您可以使用扩展定义来执行此操作,如果方法是新的,您可以使用下面的代码,如果已经存在,则创建一个覆盖方法

像这样创建UITextField的扩展

extension UITextField {
    class func attributedTextField(frame: CGRect) -> UITextField {
        let textField = UITextField(frame: frame)
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.layer.cornerRadius = 25
        textField.layer.borderColor = UIColor(r: 34, g: 140, b: 204, a: 1).CGColor
        textField.layer.borderWidth = 2.0
        textField.layer.masksToBounds = true
        /* Paddings */
        textField.leftView = UIView(frame: CGRectMake(0, 0, 25, 0))
        textField.leftViewMode = UITextFieldViewMode.Always
        textField.rightView = UIView(frame: CGRectMake(0, 0, 25, 0))
        textField.rightViewMode = UITextFieldViewMode.Always
        /* Place Holder Formating */
        textField attributes = [
                          NSForegroundColorAttributeName: UIColor(r: 34, g: 140, b: 204, a: 1),
                          NSFontAttributeName : UIFont(name: "HelveticaNeue-Thin", size: 16)! // Note the !
                          ]
        textField.attributedPlaceholder = NSAttributedString(string: "Email", attributes:attributes)
        return textField
    } 
}
let tf = UITextField.attributedTextField(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
像这样调用这个函数

extension UITextField {
    class func attributedTextField(frame: CGRect) -> UITextField {
        let textField = UITextField(frame: frame)
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.layer.cornerRadius = 25
        textField.layer.borderColor = UIColor(r: 34, g: 140, b: 204, a: 1).CGColor
        textField.layer.borderWidth = 2.0
        textField.layer.masksToBounds = true
        /* Paddings */
        textField.leftView = UIView(frame: CGRectMake(0, 0, 25, 0))
        textField.leftViewMode = UITextFieldViewMode.Always
        textField.rightView = UIView(frame: CGRectMake(0, 0, 25, 0))
        textField.rightViewMode = UITextFieldViewMode.Always
        /* Place Holder Formating */
        textField attributes = [
                          NSForegroundColorAttributeName: UIColor(r: 34, g: 140, b: 204, a: 1),
                          NSFontAttributeName : UIFont(name: "HelveticaNeue-Thin", size: 16)! // Note the !
                          ]
        textField.attributedPlaceholder = NSAttributedString(string: "Email", attributes:attributes)
        return textField
    } 
}
let tf = UITextField.attributedTextField(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
像这样创建UITextField的扩展

extension UITextField {
    class func attributedTextField(frame: CGRect) -> UITextField {
        let textField = UITextField(frame: frame)
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.layer.cornerRadius = 25
        textField.layer.borderColor = UIColor(r: 34, g: 140, b: 204, a: 1).CGColor
        textField.layer.borderWidth = 2.0
        textField.layer.masksToBounds = true
        /* Paddings */
        textField.leftView = UIView(frame: CGRectMake(0, 0, 25, 0))
        textField.leftViewMode = UITextFieldViewMode.Always
        textField.rightView = UIView(frame: CGRectMake(0, 0, 25, 0))
        textField.rightViewMode = UITextFieldViewMode.Always
        /* Place Holder Formating */
        textField attributes = [
                          NSForegroundColorAttributeName: UIColor(r: 34, g: 140, b: 204, a: 1),
                          NSFontAttributeName : UIFont(name: "HelveticaNeue-Thin", size: 16)! // Note the !
                          ]
        textField.attributedPlaceholder = NSAttributedString(string: "Email", attributes:attributes)
        return textField
    } 
}
let tf = UITextField.attributedTextField(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
像这样调用这个函数

extension UITextField {
    class func attributedTextField(frame: CGRect) -> UITextField {
        let textField = UITextField(frame: frame)
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.layer.cornerRadius = 25
        textField.layer.borderColor = UIColor(r: 34, g: 140, b: 204, a: 1).CGColor
        textField.layer.borderWidth = 2.0
        textField.layer.masksToBounds = true
        /* Paddings */
        textField.leftView = UIView(frame: CGRectMake(0, 0, 25, 0))
        textField.leftViewMode = UITextFieldViewMode.Always
        textField.rightView = UIView(frame: CGRectMake(0, 0, 25, 0))
        textField.rightViewMode = UITextFieldViewMode.Always
        /* Place Holder Formating */
        textField attributes = [
                          NSForegroundColorAttributeName: UIColor(r: 34, g: 140, b: 204, a: 1),
                          NSFontAttributeName : UIFont(name: "HelveticaNeue-Thin", size: 16)! // Note the !
                          ]
        textField.attributedPlaceholder = NSAttributedString(string: "Email", attributes:attributes)
        return textField
    } 
}
let tf = UITextField.attributedTextField(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
Swift 4:在UITextField@IBOutlet上使用UITextField扩展

Swift 4:在UITextField@IBOutlet上使用UITextField扩展


为什么不在扩展中声明方法而不是属性?你可以传递参数。@Crazyrems听起来不错,你能举个例子吗?这基本上是他的答案。您应该在框架中添加更多参数,以便根据需要自定义字段。为什么不在扩展中声明方法而不是属性?你可以传递参数。@Crazyrems听起来不错,你能举个例子吗?这基本上是他的答案。您应该在框架中添加更多参数,以根据需要自定义字段。这是我尝试使用它时出现的错误:在类型“UITextField”上使用实例成员“下划线”;您是否打算改为使用“UITextField”类型的值?这是我尝试使用它时出现的错误:在“UITextField”类型上使用实例成员“下划线”;您的意思是使用类型为“UITextField”的值吗?框架是什么:传递到attributedTextField的yourFrame变量?在textField框架中,请检查我编辑的答案,您会有更好的想法。这是您建议传递的自定义变量吗?我认为这是必需的。什么是框架:传递到attributedTextField的yourFrame变量?在textField框架中,检查我编辑的答案,您会得到更好的想法。这是您建议传递的自定义变量吗?我认为这是必须的。