UIWebView:禁用富文本编辑器的复制/剪切选项

我有一个带有contentEditable div的UIWebView,以实现某种富文本编辑器。 一旦用户select了任何一段文字,我需要在Web视图中显示的UIMenuController中修剪复制和剪切选项。

在networking上似乎有很多解决scheme,但由于某种原因,它们都不适用于我的场景。

我已经分类了UIWebView,并实现了canPerformAction:(SEL)action withSender:删除复制和剪切,但一旦用户select“select”或“全选”,出现一个新的菜单,显然,networking视图不拦截此操作,canPerform方法未被调用。

在这里输入图像说明

有没有办法减less这种情况下的行动?

我会适应你的情况的另一个答案 。

canPerformAction:实际上是在内部的UIWebDocumentView上调用,而不是通常不能UIWebDocumentViewUIWebView 。 有了一些运行时魔法,这是可能的。

我们创build一个有一个方法的类:

 @interface _SwizzleHelper : UIView @end @implementation _SwizzleHelper -(BOOL)canPerformAction:(SEL)action { //Your logic here return NO; } @end 

一旦你有一个你想控制动作的网页视图,你迭代它的滚动视图的子视图,并采取UIWebDocumentView类。 然后,我们dynamic地将上面创build的类的超类作为子视图的类(UIWebDocumentView – 但是我们不能说这是前期的,因为这是私有API),并将子视图的类replace为我们的类。

 #import "objc/runtime.h" -(void)__subclassDocumentView { UIView* subview; for (UIView* view in self.scrollView.subviews) { if([[view.class description] hasPrefix:@"UIWeb"]) subview = view; } if(subview == nil) return; //Should not stop here NSString* name = [NSString stringWithFormat:@"%@_SwizzleHelper", subview.class.superclass]; Class newClass = NSClassFromString(name); if(newClass == nil) { newClass = objc_allocateClassPair(subview.class, [name cStringUsingEncoding:NSASCIIStringEncoding], 0); if(!newClass) return; Method method = class_getInstanceMethod([_SwizzleHelper class], @selector(canPerformAction:)); class_addMethod(newClass, @selector(canPerformAction:), method_getImplementation(method), method_getTypeEncoding(method)); objc_registerClassPair(newClass); } object_setClass(subview, newClass); } 
Interesting Posts