BorderTextField.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // BorderTextField.swift
  3. // Bark
  4. //
  5. // Created by huangfeng on 2023/2/6.
  6. // Copyright © 2023 Fin. All rights reserved.
  7. //
  8. import UIKit
  9. class InsetTextField: UITextField {
  10. var insets = UIEdgeInsets.zero
  11. override func textRect(forBounds bounds: CGRect) -> CGRect {
  12. let bounds = super.textRect(forBounds: bounds)
  13. return bounds.inset(by: insets)
  14. }
  15. override func editingRect(forBounds bounds: CGRect) -> CGRect {
  16. let bounds = super.textRect(forBounds: bounds)
  17. return bounds.inset(by: insets)
  18. }
  19. override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
  20. let bounds = super.textRect(forBounds: bounds)
  21. return bounds.inset(by: insets)
  22. }
  23. }
  24. class BorderTextField: InsetTextField {
  25. var isSelecting: Bool = true {
  26. didSet {
  27. UIView.animate(withDuration: 0.3) {
  28. if self.isSelecting {
  29. self.backgroundView.borderColor = BKColor.blue.darken5
  30. self.backgroundView.shadowColor = BKColor.blue.darken5
  31. self.backgroundView.layer.shadowOpacity = 0.3
  32. }
  33. else {
  34. self.backgroundView.borderColor = BKColor.grey.lighten2
  35. self.backgroundView.shadowColor = BKColor.grey.lighten2
  36. self.backgroundView.layer.shadowOpacity = 0
  37. }
  38. }
  39. }
  40. }
  41. let backgroundView: UIView = {
  42. let view = UIView()
  43. view.backgroundColor = BKColor.white
  44. view.isUserInteractionEnabled = false
  45. view.cornerRadiusPreset = .cornerRadius3
  46. view.shadowColor = BKColor.grey.lighten2
  47. view.layer.shadowOffset = CGSize(width: 0, height: 0)
  48. view.layer.shadowRadius = 2
  49. view.layer.shadowOpacity = 0
  50. view.borderColor = BKColor.grey.lighten2
  51. view.borderWidthPreset = .border2
  52. return view
  53. }()
  54. init(title: String? = nil) {
  55. super.init(frame: CGRect.zero)
  56. self.insets = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
  57. self.textColor = BKColor.grey.darken3
  58. self.font = UIFont.systemFont(ofSize: 14)
  59. self.textAlignment = .left
  60. self.insertSubview(backgroundView, at: 0)
  61. backgroundView.snp.makeConstraints { make in
  62. make.edges.equalToSuperview()
  63. }
  64. self.delegate = self
  65. }
  66. override var placeholder: String? {
  67. didSet{
  68. self.attributedPlaceholder = NSAttributedString(string: placeholder ?? "" , attributes: [
  69. .font: self.font ?? UIFont.systemFont(ofSize: 14),
  70. .foregroundColor: BKColor.grey.darken1
  71. ])
  72. }
  73. }
  74. @available(*, unavailable)
  75. required init?(coder: NSCoder) {
  76. fatalError("init(coder:) has not been implemented")
  77. }
  78. }
  79. extension BorderTextField: UITextFieldDelegate {
  80. func textFieldDidBeginEditing(_ textField: UITextField) {
  81. self.isSelecting = true
  82. }
  83. func textFieldDidEndEditing(_ textField: UITextField) {
  84. self.isSelecting = false
  85. }
  86. }