获取文本 SwiftUI 的剩余部分

Fetch Remaining of The Text SwiftUI

我能够获取文本编辑器的第一段。但是我怎样才能获取文本的其余部分?

TextEditor(text: $newText)
            .onChange(of: newTitle, perform: { value in
                if let firstParagraph = value.components(separatedBy: CharacterSet.newlines).first {
                    selectedNote.title = firstParagraph
                }
            })
            .onChange(of: newText, perform: { value in
                if let remainingParagraph = value.components(separatedBy: CharacterSet.difference(from: newTitle)) {
                    selectedNote.text = remainingParagraph
                }
            })

您只需要 1 个 onChange。我不知道 newTitle 是什么,但你可能不需要它,因为你只有 1 TextEditor.

how can I fetch rest of the text?

dropFirst 很简单:

String(value.dropFirst(firstParagraph.count))

代码:

struct ContentView: View {
    @State var totalText = "" /// text for the TextEditor
    @State var title = "" /// demo
    @State var text = "" /// demo
    
    var body: some View {
        VStack {
            TextEditor(text: $totalText)
                .onChange(of: totalText, perform: { value in
                    
                    if let firstParagraph = value.components(separatedBy: CharacterSet.newlines).first {        

                        /// for demo, replace with selectedNote.title = firstParagraph
                        title = firstParagraph
                        text = String(value.dropFirst(firstParagraph.count)) /// get remaining text
                    }
                })
                .frame(height: 200)
                .border(Color.blue)
            
            Text("Title is \(title)")
            
            Text("Text is \(text)")
        }
    }
}

结果: