ios_app/Shared/Views/CustomTextField.swift
2025-06-11 07:54:16 +03:00

56 lines
1.6 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// CustomTextField.swift
// VolnahubApp
//
// Created by cheykrym on 11/06/2025.
//
import SwiftUI
struct CustomTextField: UIViewRepresentable {
class Coordinator: NSObject, UITextFieldDelegate {
var parent: CustomTextField
init(_ parent: CustomTextField) {
self.parent = parent
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
parent.onReturn()
return false // предотвращаем автоматический переход на новую строку
}
@objc func textFieldDidChange(_ textField: UITextField) {
parent.text = textField.text ?? ""
}
}
var placeholder: String
@Binding var text: String
var isSecure: Bool = false
var onReturn: () -> Void
func makeUIView(context: Context) -> UITextField {
let textField = UITextField()
textField.placeholder = placeholder
textField.delegate = context.coordinator
textField.borderStyle = .roundedRect
textField.autocapitalizationType = .none
textField.autocorrectionType = .no
textField.isSecureTextEntry = isSecure
textField.returnKeyType = .next
textField.addTarget(context.coordinator, action: #selector(Coordinator.textFieldDidChange(_:)), for: .editingChanged)
return textField
}
func updateUIView(_ uiView: UITextField, context: Context) {
if uiView.text != text {
uiView.text = text
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
}