Skip to content

Instantly share code, notes, and snippets.

@mcz9mm
Last active November 6, 2019 07:09
Show Gist options
  • Save mcz9mm/94371a0f37ba20c2b9338eed28235d79 to your computer and use it in GitHub Desktop.
Save mcz9mm/94371a0f37ba20c2b9338eed28235d79 to your computer and use it in GitHub Desktop.
import UIKit
@IBDesignable class PaddingTextField: UITextField {
// MARK: Properties
@IBInspectable var padding: CGPoint = CGPoint(x: 6.0, y: 0.0)
// MARK: Internal Methods
override func textRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: self.padding.x, dy: self.padding.y)
}
override func editingRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: self.padding.x, dy: self.padding.y)
}
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
return bounds.insetBy(dx: self.padding.x, dy: self.padding.y)
}
}
extension UITextField {
func setLeftPaddingPoints(_ amount:CGFloat){
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
self.leftView = paddingView
self.leftViewMode = .always
}
func setRightPaddingPoints(_ amount:CGFloat) {
let paddingView = UIView(frame: CGRect(x: 0, y: 0, width: amount, height: self.frame.size.height))
self.rightView = paddingView
self.rightViewMode = .always
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment