2

I want to paste an image in line with text like apps one note or notion do. I read somewhere that it's impossible with TextEditor and I had to use UITextView instead, so I did that but I do not know how to trigger the event PASTE to paste the picture.

Once I do that I will need to figure out how to save the whole shebang in CoreData (the text only as text, the imgs as images). But now I just need help to figure out how to paste an image into a TextEditor or UITextView. Thanks

enter image description here

struct itemDetail: View {
    
    @Environment(\.managedObjectContext) var viewContext //it's like a environemtn
    @Environment(\.presentationMode) var presentationMode
    
    
    @State var taskItem: ToDoItem?
    
    @State var textFieldText : String
    @State var textStyle = UIFont.TextStyle.body
    
    @State var textTop : String
    
    @FocusState var isFocused: Bool
    
    
    @State var textEditorHeight : CGFloat = 20
    
    var body: some View {
        VStack(alignment: .leading) {
            
            ZStack(alignment: .leading) {
                        Text(textTop)
                            .font(.system(.title))
                            .foregroundColor(.clear)
                            .padding(14)
                            .background(GeometryReader {
                                Color.clear.preference(key: ViewHeightKey.self,
                                                       value: $0.frame(in: .local).size.height)
                            })
                        
                        TextEditor(text: $textTop)
                            .font(.system(.title))
                            .frame(height: max(40,textEditorHeight))
                            .cornerRadius(10.0)
                                        .shadow(radius: 1.0)
                    }.onPreferenceChange(ViewHeightKey.self) { textEditorHeight = $0 }
        
            
            ///UI TEXTVIEW
            TextView(text: $textFieldText, textStyle: $textStyle)
                .padding(.horizontal)
    
            Button {
                //save to context

                taskItem!.detail = textFieldText
                taskItem!.desc = textTop
                
                do {
                    try viewContext.save()
                } catch {
                    let nsError = error as NSError
                    fatalError("Unresolved error \(nsError), \(nsError.userInfo)")
                }
                
                presentationMode.wrappedValue.dismiss()
                
            } label: {
                ZStack {
                    Rectangle().foregroundColor(.blue).frame(height: 40).cornerRadius(10)
                    Text("Save").font(.subheadline).foregroundColor(.white)
                }

            }
            
            Spacer()
            
        }
            
    }
    

}

//override func paste(_ sender: Any?) {
//    let textAttachment = NSTextAttachment()
//    textAttachment.image = UIPasteboard.general.image
//    textFieldText = NSAttributedString(attachment: textAttachment)
//}

struct ViewHeightKey: PreferenceKey {
    static var defaultValue: CGFloat { 0 }
    static func reduce(value: inout Value, nextValue: () -> Value) {
        value = value + nextValue()
    }
}

//textview wrpaped uikit

struct TextView: UIViewRepresentable {
 
        @Binding var text: String
        @Binding var textStyle: UIFont.TextStyle
     
        func makeUIView(context: Context) -> UITextView {
            let textView = UITextView()
            
            textView.delegate = context.coordinator
            
            textView.font = UIFont.preferredFont(forTextStyle: textStyle)
            textView.autocapitalizationType = .sentences
            textView.isSelectable = true
            textView.isUserInteractionEnabled = true
            
     
            return textView

        }
     
        func updateUIView(_ uiView: UITextView, context: Context) {
            uiView.text = text
            uiView.font = UIFont.preferredFont(forTextStyle: textStyle)
        }
    
    func makeCoordinator() -> Coordinator {
        Coordinator($text)
    }
     
    class Coordinator: NSObject, UITextViewDelegate {
        var text: Binding<String>
     
        init(_ text: Binding<String>) {
            self.text = text
        }
     
        func textViewDidChange(_ textView: UITextView) {
            self.text.wrappedValue = textView.text
        }
    }
}
Timmy
  • 4,098
  • 2
  • 14
  • 34
Nat Serrano
  • 472
  • 1
  • 4
  • 17
  • Does this answer your question? [How can I add image in UITextView?](https://stackoverflow.com/questions/38479233/how-can-i-add-image-in-uitextview) – Timmy Sep 10 '22 at 07:29
  • No, unfortunately, I just want to paste an image from clipboard to a texteditor or uitextview – Nat Serrano Sep 16 '22 at 01:11

0 Answers0