mrGott
mrGott

Reputation: 1076

Create UITextField Extension in Swift with custom styling

I have several UITextFields on the view that have to look the same. I figured out that I might create an Extension that will pre-style my text fields (those that I want to have the same style).

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
}()

so most of these attributes should be included into the extension and I'd like to be able to add couple of them to it when I declare the variable.

can you help me out? I've been searching on how to create an extension but nothing seems to work out for me.

Thank you!

Upvotes: 1

Views: 10154

Answers (3)

Pengguna
Pengguna

Reputation: 4951

Swift 4: Use UITextField Extension on UITextField @IBOutlet

@IBOutlet weak var normalTextField: UITextField!

override func viewDidLoad() {
    super.viewDidLoad()
    normalTextField.toStyledTextField()
}
extension UITextField {
    func toStyledTextField() { // Give Round Border and Left Placholder Padding
        self.layer.masksToBounds = true
        self.layer.cornerRadius = 10
        self.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 15, height: 0))
        self.leftViewMode = UITextField.ViewMode.always
    }
}

Upvotes: 0

Nirav D
Nirav D

Reputation: 72460

Create extension of UITextField like this

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
    } 
}

call this function like this

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

Upvotes: 6

Özgür Ersil
Özgür Ersil

Reputation: 7013

you can do it with extension definition , if the method is new you can use the code below, if already exist make an override method

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
    }
}

Upvotes: 6

Related Questions