Skip to content

Instantly share code, notes, and snippets.

@foxicode
Created April 16, 2020 07:14
Show Gist options
  • Select an option

  • Save foxicode/4dd3598cb1788d0030fe644801ff630c to your computer and use it in GitHub Desktop.

Select an option

Save foxicode/4dd3598cb1788d0030fe644801ff630c to your computer and use it in GitHub Desktop.
UITextField subclass with padding editable in Storyboard
import UIKit
@IBDesignable
class TextFieldWithPadding: UITextField {
@IBInspectable var paddingLeft: CGFloat = 0.0
@IBInspectable var paddingRight: CGFloat = 0.0
var padding: UIEdgeInsets {
UIEdgeInsets(top: 0, left: paddingLeft, bottom: 0, right: paddingRight)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func textRect(forBounds bounds: CGRect) -> CGRect {
bounds.inset(by: padding)
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
bounds.inset(by: padding)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
bounds.inset(by: padding)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment