PaddedTextField.swift 855 B

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // PaddedTextField.swift
  3. // LoopKitUI
  4. //
  5. // Copyright © 2018 LoopKit Authors. All rights reserved.
  6. //
  7. import UIKit
  8. class PaddedTextField: UITextField {
  9. @IBInspectable var textInset: CGSize = .zero {
  10. didSet {
  11. setNeedsLayout()
  12. invalidateIntrinsicContentSize()
  13. }
  14. }
  15. private var textInsets: UIEdgeInsets {
  16. return UIEdgeInsets(top: textInset.height, left: textInset.width, bottom: textInset.height, right: textInset.width)
  17. }
  18. override func editingRect(forBounds bounds: CGRect) -> CGRect {
  19. return bounds.inset(by: textInsets)
  20. }
  21. override func textRect(forBounds bounds: CGRect) -> CGRect {
  22. return bounds.inset(by: textInsets)
  23. }
  24. override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
  25. return bounds.inset(by: textInsets)
  26. }
  27. }