LNEditNickNamePanel.swift 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // LNEditNickNamePanel.swift
  3. // Lanu
  4. //
  5. // Created by OneeChan on 2025/12/19.
  6. //
  7. import Foundation
  8. import UIKit
  9. import SnapKit
  10. class LNEditNickNamePanel: LNPopupView {
  11. let inputField = UITextField()
  12. private let confirmButton = UIButton()
  13. var handler: ((String) -> Void)?
  14. override init(frame: CGRect) {
  15. super.init(frame: frame)
  16. setupViews()
  17. }
  18. required init?(coder: NSCoder) {
  19. fatalError("init(coder:) has not been implemented")
  20. }
  21. }
  22. extension LNEditNickNamePanel: UITextFieldDelegate {
  23. func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
  24. let currentText = textField.text ?? ""
  25. guard let range = Range(range, in: currentText) else { return false }
  26. let newText = currentText.replacingCharacters(in: range, with: string)
  27. if newText.count < currentText.count {
  28. return true
  29. }
  30. return newText.count <= LNProfileManager.nameMaxInput
  31. }
  32. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  33. textField.resignFirstResponder()
  34. return true
  35. }
  36. }
  37. extension LNEditNickNamePanel {
  38. private func setupViews() {
  39. let titleLabel = UILabel()
  40. titleLabel.font = .heading_h3
  41. titleLabel.textColor = .text_5
  42. titleLabel.text = .init(key: "A00188")
  43. container.addSubview(titleLabel)
  44. titleLabel.snp.makeConstraints { make in
  45. make.centerX.equalToSuperview()
  46. make.top.equalToSuperview().offset(16)
  47. }
  48. let inputHolder = UIView()
  49. container.addSubview(inputHolder)
  50. inputHolder.snp.makeConstraints { make in
  51. make.top.equalTo(titleLabel.snp.bottom).offset(16)
  52. make.horizontalEdges.equalToSuperview().inset(16)
  53. }
  54. let holder = UIView()
  55. holder.backgroundColor = .fill_1
  56. holder.layer.cornerRadius = 8
  57. inputHolder.addSubview(holder)
  58. holder.snp.makeConstraints { make in
  59. make.horizontalEdges.equalToSuperview()
  60. make.top.equalToSuperview()
  61. make.height.equalTo(46)
  62. }
  63. inputField.font = .body_m
  64. inputField.textColor = .text_5
  65. inputField.clearButtonMode = .whileEditing
  66. inputField.delegate = self
  67. inputField.returnKeyType = .done
  68. inputField.visibleView = inputHolder
  69. inputField.addAction(UIAction(handler: { [weak self] _ in
  70. guard let self else { return }
  71. let text = inputField.text
  72. confirmButton.isEnabled = text?.isEmpty == false
  73. if confirmButton.isEnabled, confirmButton.backgroundImage(for: .normal) == nil {
  74. confirmButton.setBackgroundImage(.primary_8, for: .normal)
  75. } else if !confirmButton.isEnabled, confirmButton.backgroundImage(for: .normal) != nil {
  76. confirmButton.setBackgroundImage(nil, for: .normal)
  77. }
  78. }), for: .editingChanged)
  79. holder.addSubview(inputField)
  80. inputField.snp.makeConstraints { make in
  81. make.horizontalEdges.equalToSuperview().inset(12)
  82. make.centerY.equalToSuperview()
  83. }
  84. let tipsLabel = UILabel()
  85. tipsLabel.font = .body_s
  86. tipsLabel.text = .init(key: "A00189", LNProfileManager.nameMaxInput)
  87. tipsLabel.textColor = .text_3
  88. inputHolder.addSubview(tipsLabel)
  89. tipsLabel.snp.makeConstraints { make in
  90. make.trailing.equalTo(holder)
  91. make.top.equalTo(holder.snp.bottom).offset(8)
  92. make.bottom.equalToSuperview()
  93. }
  94. confirmButton.setTitle(.init(key: "A00185"), for: .normal)
  95. confirmButton.setTitleColor(.text_1, for: .normal)
  96. confirmButton.titleLabel?.font = .heading_h3
  97. confirmButton.setBackgroundImage(.primary_8, for: .normal)
  98. confirmButton.layer.cornerRadius = 23.5
  99. confirmButton.clipsToBounds = true
  100. confirmButton.backgroundColor = .fill_4
  101. confirmButton.addAction(UIAction(handler: { [weak self] _ in
  102. guard let self else { return }
  103. guard let text = inputField.text else { return }
  104. dismiss()
  105. handler?(text)
  106. }), for: .touchUpInside)
  107. container.addSubview(confirmButton)
  108. confirmButton.snp.makeConstraints { make in
  109. make.horizontalEdges.equalToSuperview().inset(12)
  110. make.top.equalTo(inputHolder.snp.bottom).offset(16)
  111. make.bottom.equalToSuperview().offset(commonBottomInset)
  112. make.height.equalTo(47)
  113. }
  114. }
  115. }