【问题标题】:How can I properly group and minimize style code for UITextfields?如何正确分组和最小化 UITextfields 的样式代码?
【发布时间】:2015-09-11 20:11:48
【问题描述】:

我正在尝试严格基于登录/注册字段来实现我的文本字段的标准通用样式。

所以,我将它们都设计为相同的,但我认为我正在重用大量可以压缩并可能在变量中使用的代码?我不知道该怎么做。

它的工作方式,但我确信它可以做得比这更好。我几乎可以肯定有一种方法可以最小化此代码以获得更好的实践。

我还在学习,所以我真的很想在开发中学习更好的实践。

这是我的注册视图和字段样式的示例:

class JoinVC: UIViewController, UITextFieldDelegate {`

    @IBOutlet weak var enterEmailTextField: UITextField!
    @IBOutlet weak var enterPasswordTextField: UITextField!
    @IBOutlet weak var enterNameTextField: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Field Border Corner + Width
        self.enterEmailTextField.layer.cornerRadius = 24.0
        self.enterEmailTextField.layer.borderWidth = 1.5
        self.enterPasswordTextField.layer.cornerRadius = 24.0
        self.enterPasswordTextField.layer.borderWidth = 1.5
        self.enterNameTextField.layer.cornerRadius = 24.0
        self.enterNameTextField.layer.borderWidth = 1.5
        // ...

        // Field Placeholder ColorEnter
        var placeholderEnterEmail = NSAttributedString(string: "Enter Email", attributes: [NSForegroundColorAttributeName : UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)])
        var placeholderEnterPass = NSAttributedString(string: "Choose Password", attributes: [NSForegroundColorAttributeName : UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)])
        var placeholderEnterName = NSAttributedString(string: "Choose Username", attributes: [NSForegroundColorAttributeName : UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)])

        enterEmailTextField.layer.sublayerTransform = CATransform3DMakeTranslation(20, 0, 0);
        enterPasswordTextField.layer.sublayerTransform = CATransform3DMakeTranslation(20, 0, 0);
        enterNameTextField.layer.sublayerTransform = CATransform3DMakeTranslation(20, 0, 0);
        // ...

        // Text Field Border color
        var borderColor : UIColor = UIColor( red: 255, green: 255, blue:255, alpha: 0.8 )
        self.enterPasswordTextField.layer.borderColor = borderColor.CGColor; enterEmailTextField.attributedPlaceholder = placeholderEnterEmail;
        self.enterEmailTextField.layer.borderColor = borderColor.CGColor; enterPasswordTextField.attributedPlaceholder = placeholderEnterPass;
        self.enterNameTextField.layer.borderColor = borderColor.CGColor; enterNameTextField.attributedPlaceholder = placeholderEnterName;
    // ...


    }
}

【问题讨论】:

    标签: ios xcode swift cocoa-touch uitextfield


    【解决方案1】:

    您可以使用Extension functionality。 只需创建一个 ExtensionUITextField 并将您的代码放入该 Extension (简易教程here

    例如:

    extension UITextField
    {
        func setPlaceholderColorAndString(pholder: String, color: UIColor)
        {
            self.attributedPlaceholder = NSAttributedString(string:pholder,
                attributes:[NSForegroundColorAttributeName: color])
        }
    
        func setupField()
        {
            self.layer.cornerRadius = 24.0
            self.layer.borderWidth = 1.5
        }
    }
    

    然后在你的代码中你可以像这样使用它:

    self.enterEmailTextField.setPlaceholderColorAndString(pholder: "Enter Email", color: UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)
    self.enterEmailTextField.setupField()
    self.enterPasswordTextField.setPlaceholderColorAndString(pholder: "Enter Password", color: UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)
    self.enterPasswordTextField.setupField()
    

    等等。当然上面的 sn-p 只是为了让你了解如何使用它,你可以改进它来满足你的需求。

    【讨论】:

      【解决方案2】:

      可重用代码的简单解决方案是处理程序

        func setupTextField(textField : UITextField, placeHolderString: String)
        {
          let borderColor = UIColor( red: 1.0, green: 1.0, blue:1.0, alpha: 0.8 )
          textField.layer.cornerRadius = 24.0
          textField.layer.borderWidth = 1.5
          textField.layer.sublayerTransform = CATransform3DMakeTranslation(20, 0, 0);
          textField.layer.borderColor = borderColor.CGColor;
          textField.attributedPlaceholder = NSAttributedString(string: placeHolderString, attributes: [NSForegroundColorAttributeName : UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)])
      
        }
      
        override func viewDidLoad() {
          super.viewDidLoad()
      
          setupTextField(enterEmailTextField, placeHolderString: "Enter Email")
          setupTextField(enterPasswordTextField, placeHolderString: "Choose Password")
          setupTextField(enterNameTextField, placeHolderString: "Choose Username")
        }
      

      注意:UIColor 值必须在 0.0 到 1.0 的范围内

      【讨论】:

        【解决方案3】:

        我之前解决这个问题的方式,特别是如果项目中的所有文本字段都是:

        1. 总是具有相同的属性,
        2. 总是从情节提要中创建

        是继承UITextField 并应用-awakeFromNib 中的属性:

        class KBTextField: UITextField {
            let myAttributes = [NSForegroundColorAttributeName : UIColor(red: 255/255, green: 255/255, blue:255/255, alpha: 0.6)]
            let mySublayerTransform = CATransform3DMakeTranslation(20, 0, 0)
            let myBorderColor = UIColor( red: 255, green: 255, blue:255, alpha: 0.8 )
        
            override func awakeFromNib () {
                self.layer.sublayerTransform = mySublayerTransform
                self.layer.borderColor = myBorderColor.CGColor
                self.layer.cornerRadius = 24.0
                self.layer.borderWidth = 1.5
                self.attributedPlaceholder = NSAttributedString(string: self.placeholder!, attributes: myAttributes)
            }
        }
        

        然后您只需在故事板上设置类(在本例中为KBTextField),它会自动处理您的所有属性。

        这样,您可以确保应用中的所有KBTextFields 看起来都相同,只要您通过情节提要创建它们。

        【讨论】:

        • 这对我来说非常有效,因为我可以在我希望定位的字段上重用该类!谢谢
        猜你喜欢
        • 1970-01-01
        • 2011-03-29
        • 2021-08-10
        • 1970-01-01
        • 2020-02-18
        • 1970-01-01
        • 2011-10-06
        • 1970-01-01
        • 2012-04-04
        相关资源
        最近更新 更多