iphone - UIWebView: Disable copy/cut options for a rich text editor -
i have uiwebview contenteditable div in order implement kind of rich text editor. need trimm copy & cut options in uimenucontroller appears in web view once user selects piece of text.
there seems lot of solutions around web, reason, non of them applies scenario.
i've subclassed uiwebview , implemented canperformaction:(sel)action withsender:
remove copy , cut, once user chooses "select" or "select all", new menu appears, , apparently, web view not intercept action , canperform method not being called.
is there way trimm actions cases?
i adapt another answer of mine case.
the canperformaction:
called on internal uiwebdocumentview
instead of uiwebview
, cannot subclass. runtime magic, it's possible.
we create class has 1 method:
@interface _swizzlehelper : uiview @end @implementation _swizzlehelper -(bool)canperformaction:(sel)action { //your logic here return no; } @end
once have web view want control actions of, iterate scroll view's subviews , take uiwebdocumentview
class. dynamically make superclass of class created above subview's class (uiwebdocumentview - cannot upfront because private api), , replace subview's class our class.
#import "objc/runtime.h" -(void)__subclassdocumentview { uiview* subview; (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); }
Comments
Post a Comment