Files
sodalive-ios/SodaLive/Sources/CustomView/ChatTextFieldView.swift
Yu Sung 70003af82b feat(live-room): 채팅창 얼리기 기능을 추가한다
채팅 입력 제어와 룸 상태 동기화를 통합해 지연 입장자도 동일 상태를 적용한다.
2026-03-19 18:20:13 +09:00

76 lines
2.4 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
}
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return parent.isEnabled
}
@objc func textDidChange(_ textField: UITextField) {
parent.text = textField.text ?? ""
}
}
@Binding var text: String
var placeholder: String
var isEnabled: Bool = true
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.preMedium.rawValue, size: 13.3)
textField.returnKeyType = .send
textField.isEnabled = isEnabled
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) {
context.coordinator.parent = self
uiView.text = text
uiView.isEnabled = isEnabled
if !isEnabled && uiView.isFirstResponder {
uiView.resignFirstResponder()
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
}
#Preview {
ChatTextFieldView(text: .constant(""), placeholder: "채팅을 입력하세요") {}
}