Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding top padding to UITextField

I'm trying to add top padding to a text field, but could not find anything useful, all solution were about left and right padding which I already know about. To make things clear, I want to make it like the image below.

Note: I used label for "Description" and I have put it within the text field, but label and input overlapped.

Text Field Design

like image 466
Abdullah Avatar asked Dec 04 '22 20:12

Abdullah


1 Answers

Use a customTextField class

class CustomTextField: UITextField {

    let padding = UIEdgeInsets(top: 15, left: 0, bottom: 0, right: 0);

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return UIEdgeInsetsInsetRect(bounds, padding)
    }
}

Swift 4 version, as per Micah's answer below:

class CustomTextField: UITextField {

    let padding = UIEdgeInsets(top: 15, left: 0, bottom: 0, right: 0);

    override func textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }

    override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }

    override func editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.inset(by: padding)
    }
}
like image 181
Manee.O.H Avatar answered Jun 11 '23 10:06

Manee.O.H