Hey folks! I’m working on an app and I’m trying to figure out how to add images right in the middle of text, kind of like how OneNote or Notion does it. I’ve been messing around with TextEditor
, but I heard it might not work for this. So I switched to UITextView
, but now I’m stuck on how to actually paste the image in there.
I’m also wondering how I’ll save all this stuff in CoreData later. But right now, I just really need help with getting images into either TextEditor
or UITextView
. Anyone have any ideas?
Here’s a bit of my code to give you an idea of what I’m working with:
struct NoteView: View {
@State private var noteText = ""
@State private var titleText = ""
var body: some View {
VStack {
TextField("Title", text: $titleText)
.font(.title)
.padding()
TextView(text: $noteText)
.padding()
Button(action: saveNote) {
Text("Save Note")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(10)
}
}
}
func saveNote() {
// TODO: Save note with text and images
}
}
struct TextView: UIViewRepresentable {
@Binding var text: String
func makeUIView(context: Context) -> UITextView {
let textView = UITextView()
textView.delegate = context.coordinator
return textView
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = text
}
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UITextViewDelegate {
var parent: TextView
init(_ parent: TextView) {
self.parent = parent
}
func textViewDidChange(_ textView: UITextView) {
parent.text = textView.text
}
}
}
Any help would be awesome. Thanks!