简体   繁体   English

如何在UITextfield中禁用UIMenuController的复制和定义UIMenuItems

[英]How to disable Copy & Define UIMenuItems of UIMenuController in UITextfield

I am implementing custom UIMenuController and trying to figure out. 我正在实现自定义UIMenuController并试图找出UIMenuController How can I legally disable "Copy" and "Define" UIMenuItems of UIMenuController in UITextfield ? 我怎样才能合法地禁用“复制”和“定义”的UIMenuItems UIMenuControllerUITextfield Textfield is not editable. Textfield不可编辑。 I tried to disable "Copy" using: 我尝试使用以下命令禁用“复制”:

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender 
{
   if (action == @selector(copy:))
    {
        return NO;
    }

    return [super canPerformAction:action withSender:sender];
}


- (IBAction)tapTextViewGesture:(id)sender {

  UIMenuItem *myItem1 = [[UIMenuItem alloc] initWithTitle:@"myItem1" action:@selector(myItem1Pressed:)];
  UIMenuItem *myItem2 = [[UIMenuItem alloc] initWithTitle:@"myItem2" action:@selector(myItem2Pressed:)];
  UIMenuItem *myItem3 = [[UIMenuItem alloc] initWithTitle:@"myItem3" action:@selector(myItem3Pressed:)];

    // Access the application's shared menu
    UIMenuController *menu = [UIMenuController sharedMenuController];

    [menu setMenuItems:[NSArray arrayWithObjects:myItem1,myItem2,myItem3, nil]];

    CGRect menuRect = CGRectMake(20, 50, 200, 0);


    // Show the menu from the cursor's position
    [menu setTargetRect:menuRect inView:self.view];


    [menu setMenuVisible:YES animated:YES];
}

But the menu is still showing "Copy" and "Define" UIMenuItems . 但菜单仍显示“复制”和“定义” UIMenuItems How can I disable them, leaving only my items? 如何禁用它们,只留下我的物品?

Finally solved it by subclassing UITextView (created custom class for it) and just added 最后通过UITextView (为它创建自定义类)并刚刚添加来解决它

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{

    if (action == @selector(copy:))
    {
        return NO;
    }

    return NO;
}

inside of .m file of my custom TextView subclass. 在我的自定义TextView子类的.m文件中。

After that "Copy" doesn't appear any more, with or without [menu update] ; 之后,“复制”不再出现,有或没有[menu update] ;

Implement this instance method in viewController.m: 在viewController.m中实现此实例方法:

**- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if ([_targetTextField isFirstResponder]) {
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            [[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
        }];
    }
    return [super canPerformAction:action withSender:sender];
}**

This method checks if targeted text field is the first responder or not. 此方法检查目标文本字段是否是第一个响应者。 If it is, NSOperationQueue creates a separate thread for sharedMenuController operation, set its visibility and animation as no so that it couldn't work for copy, paste,etc. 如果是,NSOperationQueue为sharedMenuController操作创建一个单独的线程,将其可见性和动画设置为no,使其无法用于复制,粘贴等。 The return statement calls UIResponder's canPerformAction method to inform for the same to implement it. return语句调用UIResponder的canPerformAction方法来通知它实现它。

You can do this by making a class by subclassing UITextField class and overriding its canPerformAction:: method. 您可以通过UITextField类并重写其canPerformAction::方法来创建一个类。

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    // Returning 'NO' here disables all actions on textfield
    return NO;
}

Swift 4.2. Swift 4.2。 && Xcode 10 that works for me: && Xcode 10对我有用

public extension UITextView {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        // Requested action to prevent:
        guard action != #selector(copy(_:)) else { return false }      // disabling copy

        // Further actions to prevent:
        // guard action != #selector(cut(_:)) else { return false }    // disabling cut
        // guard action.description != "_share:" else { return false } // disabling share
        return super.canPerformAction(action, withSender: sender)
    }
}

In order to make this work you must create a subclass of UITextField / UITextView and make sure to call super on super.canPerformAction(_:withSender:) ! 为了完成这项工作,你必须创建一个UITextField / UITextView的子类,并确保在super.canPerformAction(_:withSender:)上调用super!

Add this after you set your menu items 设置菜单项后添加此项

[menu update];

I hope this solves your query. 我希望这能解决你的问题。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在UITextView的UIMenuController中禁用默认的UIMenuItems - Cannot Disable Default UIMenuItems in UIMenuController in UITextView 在Swift中禁用UIMenuController中的UIMenuItems的闪烁/闪烁 - Disable the flickering/blinking of UIMenuItems in UIMenuController in Swift 如何从UITextfields UIMenuController删除不需要的UIMenuItems? - How to remove unwanted UIMenuItems from UITextfields UIMenuController? 如何从UIMenuController中删除Copy,Select All,define menuItem - How to remove Copy,Select All,Define menuItem from UIMenuController 在UIMenuController iOS的开头附加自定义UIMenuItems - Append custom UIMenuItems at the beginning of UIMenuController iOS 由于在应用程序的其他位置使用了UIMenuController,因此UISearchBar显示错误的UIMenuItems - UISearchBar shows wrong UIMenuItems due to use of UIMenuController elsewhere in app 在UITextField的editDidBegin中显示UIMenuController - Display UIMenuController in editingDidBegin of a UITextField 如何在ios7中的UITextfield中禁用复制/粘贴选项 - How to disable copy/paste option in UITextfield in ios7 如何以编程方式从 UITextField 禁用复制粘贴选项 - How to disable copy paste option from UITextField programmatically 如何禁用 UITextField 的选择? - How to disable the selection of a UITextField?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM