在SwiftUI聊天应用中单击发送按钮时,如何清除自定义UITextView中的文本?

尼克兹·乔恩

我对UIKit的了解有限。因此,我遵循了有关创建聊天室的教程。遵循代码。唯一的问题是,发送消息后我无法重置聊天文本。短信正在重置。但这并未反映在名为的Custom UITextView内部MessageField我正在使用Xcode 12.3。

执行发送按钮动作时如何清除UITextView的文本?

MessageField.swift


import SwiftUI

struct MessageField: UIViewRepresentable {
    
    @Binding var text: String
    @Binding var height: CGFloat
    var placeholder = "Message"
    
    func makeCoordinator() -> Coordiator {
        return MessageField.Coordiator(parent: self, placeholder: placeholder)
    }
    
    func makeUIView(context: Context) -> UITextView {
        let view = UITextView()
        view.isEditable = true
        view.isScrollEnabled = true
        view.text = placeholder
        view.font = .systemFont(ofSize: 18)
        view.textColor = .lightGray
        view.backgroundColor = .clear
        view.delegate = context.coordinator
        return view
    }
    
    func updateUIView(_ uiView: UITextView, context: Context) {
        DispatchQueue.main.async {
            self.height = uiView.contentSize.height
        }
    }
    
    class Coordiator: NSObject, UITextViewDelegate {
        var parent: MessageField
        var placeholder: String
        
        init(parent: MessageField, placeholder: String) {
            self.parent = parent
            self.placeholder = placeholder
        }
        
        func textViewDidBeginEditing(_ textView: UITextView) {
            if self.parent.text == "" {
                textView.text = ""
                textView.textColor = .label
            }
        }
        
        func textViewDidEndEditing(_ textView: UITextView) {
            if self.parent.text == "" {
                textView.text = placeholder
                textView.textColor = .lightGray
            }
        }
        
        func textViewDidChange(_ textView: UITextView) {
            DispatchQueue.main.async {
                self.parent.height = textView.contentSize.height
                self.parent.text = textView.text
            }
        }
    }
}

ChatField.swift


import SwiftUI

struct ChatField: View {
    
    @Binding var text: String
    @State var height: CGFloat = 0
    var sendText: () -> ()
    
    var body: some View {
        HStack(alignment: .bottom) {
            MessageField(text: self.$text, height: self.$height)
                .frame(height: self.height < 150 ? self.height : 150)
                .padding(.horizontal)
                .background(Color(.systemBackground))
                .cornerRadius(10)
            Button(action: {
                self.sendText()
                self.text = ""
                print("Message sent")
            }, label: {
                Image(systemName: "paperplane.fill")
                    .resizable()
                    .scaledToFit()
                    .padding(12)
                    .frame(height: 38, alignment: .center)
                    .foregroundColor(.foregroundLabel)
            })
            .background(Color(.systemBackground))
            .clipShape(RoundedRectangle(cornerRadius: 10))
        }
        .padding(.horizontal)
        .padding(.vertical, 5)
    }
}

ChatRoom.swift


import SwiftUI

struct ChatRoom: View {
    
    @State var text = ""
    @State var height: CGFloat = 0
    
    var body: some View {
        VStack(spacing: 0) {
            HStack {
                Text("Chat Room")
                    .font(.title)
                    .fontWeight(.bold)
                
                Spacer()
            }
            .padding()
            .background(Color(.systemBackground))
            
            ScrollView(.vertical, showsIndicators: false) {
                VStack(alignment: .leading) {
                    ForEach(0 ..< 60) { item in
                        Text("Hello \(item+1)")
                    }
                }
                .padding(.horizontal)
                .frame(maxWidth: .infinity, alignment: .leading)
            }
            
            ChatField(text: $text, height: height) {
                print("Sending following text:")
                print(self.text)
            }
            .background(Color(.systemBackground).edgesIgnoringSafeArea(.bottom))
        }
        .background(Color(.systemGroupedBackground))
        .onTapGesture {
            UIApplication.shared.windows.first?.rootViewController?.view.endEditing(true)
        }
    }
}
他的脾气

UIViewRepresentable.updateUIView绑定修改时被调用,所以你需要申请外部修改数据(text在此功能在你的情况下),如

func updateUIView(_ uiView: UITextView, context: Context) {
    uiView.text = self.text                                // << here !!
    DispatchQueue.main.async {
        self.height = uiView.contentSize.height
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在自定义适配器中更改按钮单击时的按钮文本

在 Android 中单击时更新包含在自定义通知中的按钮的文本

如何防止键盘在单击发送按钮时像whatsapp一样隐藏,并持续关注Android中的编辑文本

在UITextView中按下返回按钮时,如何调用自定义函数?

自定义dojo小部件在单击按钮时不会清除文本字段

单击以在WooCommerce管理订单列表中显示文本时隐藏自定义操作按钮

在Swift OS X应用程序中单击按钮时更改“自定义视图”图形

单击 r 中的操作按钮时的自定义消息

SwiftUI列表中的自定义按钮

如何在SwiftUI中自定义文本

单击清除按钮时如何清除 WPF 中的文本框

单击按钮时如何应用 ag-grid 自定义过滤器

AngularJS,在不使用按钮的字段中单击时清除文本

单击按钮Xamarin时如何添加自定义视图

如何在iOS7中自定义UISearchBar(清除按钮)

当我在android编程中单击按钮时如何清除文本视图

在自定义用户控件中重定向按钮单击时出现设计器错误

在Android中单击“按钮”时关闭自定义对话框

SwiftUI中NavigationView导航栏的自定义后退按钮

swiftui:自定义按钮在ListView中不起作用

单击时清除“文本”框中的文本

如何在有角度的材质中定义自定义主题以控制按钮文本的颜色?

Android-如何在自定义警报对话框中检查按钮单击?

如何使用快速代码在单击按钮的自定义UITableViewCell中显示UIAlertController?

如何防止单击Angular 4中的自定义按钮组件

React Native 抽屉导航:如何通过单击特定场景中的自定义按钮打开抽屉?

如何通过单击android中的自定义按钮来打开搜索?

如何在Jodit中创建自定义按钮以将文本包装在代码标签中?

OSX中的SwiftUI自定义文本字段