How can I override paste so I can paste images in a UITextView?

Viewed 31

I am trying to paste images in a UITextView so I can have text + images in the same UITextView, I read somewhere else I need to override the paste function as it doesn't accept images for text, but I did that but I got an error "Method does not override any method from its superclass"

any idea how to override paste so I can past images in a TextView?

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
        }
        
        override func paste(_ sender: Any?) {
            let textAttachment = NSTextAttachment()
            textAttachment.image = UIPasteboard.general.image
            //attributedText = NSAttributedString(attachment: textAttachment)
            let attributedText = NSAttributedString(attachment: textAttachment)
        }
        
    }
}

0 Answers
Related