2016-08-09 52 views
1

我在視圖上有幾個UITextField必須看起來相同。我發現我可能會創建一個擴展,它將預先設置我的文本字段(那些我想擁有相同樣式的字段)。在Swift中使用自定義樣式創建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 
}() 

所以大部分這些屬性應納入推廣和我希望能夠他們夫婦添加到它時,我聲明變量。

你能幫我嗎?我一直在尋找如何創建一個擴展,但似乎沒有爲我工作。

謝謝!

+0

爲什麼不聲明方法而不是擴展中的屬性?你將能夠傳遞參數。 – Crazyrems

+0

@Crazyrems聽起來不錯,請你舉個例子嗎? – mrGott

+0

這基本上是他的答案http://stackoverflow.com/a/38850484/1392046。您應該在框架中添加更多參數以根據需要定製您的字段。 – Crazyrems

回答

3

這樣

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 
    } 
} 
創建的 UITextField extension

這樣稱呼此功能

let tf = UITextField.attributedTextField(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) 
+0

什麼是框架:yourFrame變量傳遞給JavasTextField? – mrGott

+0

textField框架,檢查我編輯的答案,你會得到連擊的想法。 –

+0

這是您建議通過的自定義變量嗎?我認爲這是必需的。 – mrGott

3

你可以用extension定義做,如果該方法是新的,你可以使用下面的代碼,如果已經存在做出override方法

extension UITextField { 

    func underlined(){ 
     let border = CALayer() 
     let width = CGFloat(1.0) 
     border.borderColor = UIColor.lightGrayColor().CGColor 
     border.frame = CGRect(x: 0, y: self.frame.size.height - width, width: self.frame.size.width, height: self.frame.size.height) 
     border.borderWidth = width 
     self.layer.addSublayer(border) 
     self.layer.masksToBounds = true 
    } 
} 
+1

這是我嘗試使用它時的錯誤:在'UITextField'類型上使用實例成員'加下劃線';你的意思是使用'UITextField'類型的值嗎? – mrGott

相關問題