64 lines
2.0 KiB
Swift
64 lines
2.0 KiB
Swift
//
|
|
// ChatTextFieldView.swift
|
|
// SodaLive
|
|
//
|
|
// Created by klaus on 10/24/24.
|
|
//
|
|
|
|
import SwiftUI
|
|
import UIKit
|
|
|
|
struct ChatTextFieldView: UIViewRepresentable {
|
|
class Coordinator: NSObject, UITextFieldDelegate {
|
|
var parent: ChatTextFieldView
|
|
|
|
init(parent: ChatTextFieldView) {
|
|
self.parent = parent
|
|
}
|
|
|
|
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
|
|
// "Send" 버튼이 눌리면 액션 실행
|
|
parent.onSend()
|
|
return true
|
|
}
|
|
|
|
@objc func textDidChange(_ textField: UITextField) {
|
|
parent.text = textField.text ?? ""
|
|
}
|
|
}
|
|
|
|
@Binding var text: String
|
|
var placeholder: String
|
|
var onSend: () -> Void
|
|
|
|
func makeUIView(context: Context) -> UITextField {
|
|
let textField = UITextField(frame: .zero)
|
|
textField.delegate = context.coordinator
|
|
textField.borderStyle = .roundedRect
|
|
textField.placeholder = placeholder
|
|
textField.autocorrectionType = .no
|
|
textField.autocapitalizationType = .none
|
|
textField.borderStyle = .none
|
|
textField.backgroundColor = UIColor(hex: "222222")
|
|
textField.textColor = UIColor(hex: "BBBBBB")
|
|
textField.tintColor = UIColor(hex: "3BB9F1")
|
|
textField.font = UIFont(name: Font.medium.rawValue, size: 13.3)
|
|
textField.returnKeyType = .send
|
|
textField.setContentHuggingPriority(.defaultLow, for: .horizontal) // 우선순위 낮추기
|
|
textField.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) // 우선순위 낮추기
|
|
textField.addTarget(context.coordinator, action: #selector(Coordinator.textDidChange(_:)), for: .editingChanged)
|
|
return textField
|
|
}
|
|
|
|
func updateUIView(_ uiView: UITextField, context: Context) {
|
|
uiView.text = text
|
|
}
|
|
|
|
func makeCoordinator() -> Coordinator {
|
|
return Coordinator(parent: self)
|
|
}
|
|
}
|
|
#Preview {
|
|
ChatTextFieldView(text: .constant(""), placeholder: "채팅을 입력하세요") {}
|
|
}
|