如何使 UITextView 能够接收粘贴的图像
How to enable UITextView to receive pasted images
我需要支持将图像粘贴到 UITextView
。将图像复制到剪贴板后,“Paste
”选项似乎不会弹出。当剪贴板上有文本时它会执行。
这是在自定义 UITextView
中覆盖 paste
选项的方法。但我需要有关如何获得显示开始选项的帮助...
// This gets called when user presses menu "Paste" option
- (void)paste:(id)sender{
UIImage *image = [UIPasteboard generalPasteboard].image;
if (image) {
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = image;
NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:textAttachment];
self.attributedText = imageString;
} else {
// Call the normal paste action
[super paste:sender];
}
}
我遇到了一些相关问题,但它们对像我这样没有经验的开发人员没有帮助:
How to get UIMenuController work for a custom view?,
我回答了我自己的问题。您所要做的就是通过覆盖此 UITextView 方法让 UITextView 说 "I can receive pasted images":
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:) && [UIPasteboard generalPasteboard].image)
return YES;
else
return [super canPerformAction:action withSender:sender];
}
不客气。
谢谢@Matt,你的回答对我有帮助。只是扩展您的答案,这可能会对某些人有所帮助,
子类化 UITextview,当粘贴板中有图像时长按显示粘贴选项。
class MyTextView:UITextView {
var onPasteImage:(()->Void)?
override func awakeFromNib() {
super.awakeFromNib()
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(paste(_:)) && UIPasteboard.general.image != nil {
return true
}else{
return super.canPerformAction(action, withSender: sender)
}
}
override func paste(_ sender: Any?) {
super.paste(sender)
if UIPasteboard.general.image != nil {
onPasteImage?()
}
}
}
并等待 onPasteImage
在文本视图中点击粘贴时调用闭包,
inputFieldForReply.textView.onPasteImage = { [weak self] in
if let image = UIPasteboard.general.image {
// Process pasted image
}
}
我需要支持将图像粘贴到 UITextView
。将图像复制到剪贴板后,“Paste
”选项似乎不会弹出。当剪贴板上有文本时它会执行。
这是在自定义 UITextView
中覆盖 paste
选项的方法。但我需要有关如何获得显示开始选项的帮助...
// This gets called when user presses menu "Paste" option
- (void)paste:(id)sender{
UIImage *image = [UIPasteboard generalPasteboard].image;
if (image) {
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = image;
NSAttributedString *imageString = [NSAttributedString attributedStringWithAttachment:textAttachment];
self.attributedText = imageString;
} else {
// Call the normal paste action
[super paste:sender];
}
}
我遇到了一些相关问题,但它们对像我这样没有经验的开发人员没有帮助:
How to get UIMenuController work for a custom view?,
我回答了我自己的问题。您所要做的就是通过覆盖此 UITextView 方法让 UITextView 说 "I can receive pasted images":
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:) && [UIPasteboard generalPasteboard].image)
return YES;
else
return [super canPerformAction:action withSender:sender];
}
不客气。
谢谢@Matt,你的回答对我有帮助。只是扩展您的答案,这可能会对某些人有所帮助,
子类化 UITextview,当粘贴板中有图像时长按显示粘贴选项。
class MyTextView:UITextView {
var onPasteImage:(()->Void)?
override func awakeFromNib() {
super.awakeFromNib()
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(paste(_:)) && UIPasteboard.general.image != nil {
return true
}else{
return super.canPerformAction(action, withSender: sender)
}
}
override func paste(_ sender: Any?) {
super.paste(sender)
if UIPasteboard.general.image != nil {
onPasteImage?()
}
}
}
并等待 onPasteImage
在文本视图中点击粘贴时调用闭包,
inputFieldForReply.textView.onPasteImage = { [weak self] in
if let image = UIPasteboard.general.image {
// Process pasted image
}
}