我正在 SwiftUI 中工作,并在聊天模块中实现 RichEditorView,用户可以在其中发送消息和编辑消息。 我在这两部分都面临问题
1 - 当我发送消息时,我的输入字段没有变空,意味着编辑器没有重新加载。
2 - 当我编辑数据时,该字段中的数据未重新填充,这又是重新加载的问题。
即使 $text 正在接收更新的 html 内容,因为发送按钮已启用,但数据未填充。
有人可以帮助如何重新加载编辑器帖子发送消息以使其无法编辑以使其重新填充。
我使用过这样的编辑器视图。
struct RichTextEditor: UIViewRepresentable {
class Coordinator: RichEditorDelegate, RichEditorToolbarDelegate {
var parent: RichTextEditor
var isDefaultFocus: Bool
@Binding var colorPickerType: ColorPickerType?
init(_ parent: RichTextEditor, isDefaultFocus: Bool = false, colorPickerType: Binding<ColorPickerType?>) {
self.parent = parent
self.isDefaultFocus = isDefaultFocus
self._colorPickerType = colorPickerType
NotificationCenter.default.addObserver(self, selector: #selector(colorPicked(notification:)), name: AppNotifications.colorPicked, object: nil)
}
func richEditorTookFocus(_ editor: RichEditorView) {
}
func richEditorLostFocus(_ editor: RichEditorView) {
}
func richEditor(_ editor: RichEditorView, contentDidChange content: String) {
parent.htmlText = content
}
func richEditorToolbarChangeTextColor(_ toolbar: RichEditorToolbar) {
colorPickerType = .text
}
func richEditorToolbarChangeBackgroundColor(_ toolbar: RichEditorToolbar) {
colorPickerType = .background
}
func richEditorToolbarInsertLink(_ toolbar: RichEditorToolbar) {
if let hasSelection = toolbar.editor?.hasRangeSelection, hasSelection, let href = toolbar.editor?.runJS("document.getSelection().getRangeAt(0).toString()"){
toolbar.editor?.insertLink(href, title: "Link")
}
}
func richEditorDidLoad(_ editor: RichEditorView) {
editor.setEditorBackgroundColor(UIColor(parent.editorBackgroundColor))
editor.setEditorFontColor(UIColor(Color.primary))
if isDefaultFocus {
editor.focus()
}
}
func richEditor(_ editor: RichEditorView, heightDidChange height: Int) {
withAnimation {
if CGFloat(height) > parent.maxHeight {
parent.height = parent.maxHeight
} else if CGFloat(height) < parent.minHeight {
parent.height = parent.minHeight
} else if CGFloat(height) != parent.height {
parent.height = CGFloat(height)
}
}
}
@objc func colorPicked(notification: NSNotification) {
if let colorPickerType = notification.userInfo?["colorPickerType"] as? ColorPickerType, let selectedColor = notification.userInfo?["color"] as? UIColor {
switch colorPickerType {
case .text :
self.parent.toolbar.editor?.setTextColor(selectedColor)
case .background :
self.parent.toolbar.editor?.setTextBackgroundColor(selectedColor)
}
}
}
}
func makeCoordinator() -> Coordinator {
Coordinator(self, isDefaultFocus: isDefaultFocus, colorPickerType: $colorPickerType)
}
@Binding var htmlText: String
@Binding var colorPickerType: ColorPickerType?
var isDefaultFocus : Bool
var minHeight: CGFloat
@Binding var height: CGFloat
var maxHeight: CGFloat
let toolbar: RichEditorToolbar = RichEditorToolbar(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
var editorBackgroundColor = Color.red
func makeUIView(context: Context) -> RichEditorView {
let editor = RichEditorView(frame: .zero)
editor.delegate = context.coordinator
editor.html = htmlText
editor.isScrollEnabled = true
editor.placeholder = "Write something..."
toolbar.options = [
RichEditorDefaultOption.bold,
RichEditorDefaultOption.italic,
RichEditorDefaultOption.strike,
RichEditorDefaultOption.underline,
RichEditorDefaultOption.textColor,
RichEditorDefaultOption.textBackgroundColor,
RichEditorDefaultOption.link,
RichEditorDefaultOption.orderedList,
RichEditorDefaultOption.unorderedList,
RichEditorDefaultOption.indent,
RichEditorDefaultOption.outdent,
RichEditorDefaultOption.alignLeft,
RichEditorDefaultOption.alignCenter,
RichEditorDefaultOption.alignRight,
]
toolbar.editor = editor
toolbar.delegate = context.coordinator
editor.inputAccessoryView = toolbar
return editor
}
func updateUIView(_ uiView: RichEditorView, context: Context) { }
}
我正在使用库链接:https://github.com/cjwirth/RichEditorView
我希望在发送数据时将输入字段清空,并希望在点击编辑时重新填充内容。
可以通过通知来完成。 发送消息/编辑消息时发送通知并接收通知以使输入框重新加载。
类似这样的:
NotificationCenter.default.post(name: AppNotifications.reloardEditorInputField, object: nil, userInfo: ["htmlText": htmlText])