Prakash Raman
Prakash Raman

Reputation: 13923

Position the type-able area in UITextField

By default, the type-able area of a UITextField get positioned "vertical-center", like so

enter image description here

How can I get the type-able area to be positioned to the bottom but with a padding/offset from the bottom ?

Upvotes: 0

Views: 69

Answers (1)

Wallace Campos
Wallace Campos

Reputation: 1301

You can override textRectForBounds(_:), placeholderRectForBounds(_:) and editingRectForBounds(_:) methods of UITextField to customize the text position and combine with contentVerticalAlignment set to .Bottom.

Say that you want your text to have 10 points padding:

class CustomTextField: UITextField {

    @IBInspectable var padding: CGFloat = 10.0

    required init?(coder aDecoder: NSCoder){
      super.init(coder: aDecoder)
      contentVerticalAlignment = .Bottom  
    }

    override func textRectForBounds(bounds: CGRect) -> CGRect {

      return CGRectInset(bounds, padding, padding)
    }

    override func placeholderRectForBounds(bounds: CGRect) -> CGRect {

      return self.textRectForBounds(bounds)
    }

    override func editingRectForBounds(bounds: CGRect) -> CGRect {

      return self.textRectForBounds(bounds)
    }
}

Upvotes: 1

Related Questions