如何正确分组和最小化UITextfields的样式代码?

时间:2015-06-25 09:39:53

标签: ios xcode swift cocoa-touch uitextfield

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

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

它的工作方式,但我确信它可以比这更好。我几乎可以肯定,有一种方法可以最大限度地减少这些代码以实现更好的实践。

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

以下是我注册视图的示例&田野的造型:

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;
    // ...


    }
}

3 个答案:

答案 0 :(得分: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之间

答案 1 :(得分:2)

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

  1. 总是会有相同的属性,
  2. 始终从故事板创建
  3. 是子类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),它会自动处理你的所有属性。

    这样,只要您通过故事板创建它们,就可以确保应用中的所有KBTextField看起来都相同。

答案 2 :(得分:1)

您可以使用Extension functionality。 只需在代码Extension中创建一个UITextField 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()

等等。当然上面的代码片段只是为了让您了解如何使用它,您可以根据自己的需要进行大量改进。