iOS开发之-UIMenuController

作者: coderboy001 | 来源:发表于2016-03-16 21:13 被阅读1744次

    在iOS应用中,我们经常会做一些跟输入内容相关的操作,由于对绝大多数用户来说打字都是一件比较痛苦的事情,因此有没有一种可以让我们的用户以偷懒的方式完成内容的输入呢???

    非常幸运的是,apple给我们提供了这种偷懒的功能,让用户可以通过"command+c","command+v"的方式来完成内容的输入,完成这种偷懒功能的主角就是UIMenuController.

    Paste_Image.png

    在iOS的控件中,如下的控件已经默认支持UIMenuViewController:
    1.UITextFild

    Paste_Image.png

    2.UITextView

    Paste_Image.png

    3.UIWebView...

    Paste_Image.png

    这么好用的功能只有这三个控件能玩儿么?? 我们自定义的控件可以实现这样的功能么??
    经研究发现,答案是YES的,现在我将以UILabel为例子进行说明

    要使用UIMenuController指向某一个控件,就必须让该控件成为第一响应者
    1.让该控件成为第一响应者

    [self becomeFirstResponder];
    

    2.注意:并不是调用了上面一句代码的所有控件都能成为第一响应者
    因此我们还得在这个控件中添加:

    - (BOOL)canBecomeFirstResponder
    {  
        return YES;
    }
    

    3.控制菜单上显示什么样的菜单项

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

    4.让菜单显示出来

    [menuVc setTargetRect:self.frame inView:self.superview];
    [menuVc setMenuVisible:YES animated:YES];
    

    5.处理菜单上菜单项的点击事件

    - (void)copy:(UIMenuController *)menu
    {
        UIPasteboard *board = [UIPasteboard generalPasteboard];
        board.string = self.text;
        
    }
    
    - (void)paste:(UIMenuController *)menu
    {
        UIPasteboard *board = [UIPasteboard generalPasteboard];
        self.text = board.string;;
    }
    
    - (void)cut:(UIMenuController *)menu
    {
        [self copy:menu];
        self.text = nil;
    }
    
    Paste_Image.png
    #import "LXBLabel.h"
    
    @implementation LXBLabel
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        self = [super initWithFrame:frame];
        if (self) {
            [self setup];
        }
        return self;
    }
    
    - (void)awakeFromNib
    {
        [self setup];
    }
    
    - (void)setup
    {
        self.userInteractionEnabled = YES;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickLabel)]];
    }
    
    - (void)clickLabel
    {   
        [self becomeFirstResponder];
        UIMenuController *menuVc = [UIMenuController sharedMenuController];
    
    // 控制好menu的显示与隐藏
    UIMenuController *menuVc = [UIMenuController sharedMenuController];
        if (menuVc.isMenuVisible) {
            [menuVc setMenuVisible:NO animated:YES];
        }
        [menuVc setTargetRect:self.frame inView:self.superview];
        [menuVc setMenuVisible:YES animated:YES];
    }
    
    - (BOOL)canBecomeFirstResponder
    {
       // 明确该控件可以成为第一响应者 
        return YES;
    }
    
    // 该控件可以执行哪些动作
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
    {
        if (action == @selector(copy:) || action == @selector(paste:) || action == @selector(cut:)) {
            return YES;
        }
        return NO;
    }
    
    - (void)copy:(UIMenuController *)menu
    {
        UIPasteboard *board = [UIPasteboard generalPasteboard];
        board.string = self.text;
        
    }
    
    - (void)paste:(UIMenuController *)menu
    {
        UIPasteboard *board = [UIPasteboard generalPasteboard];
        self.text = board.string;;
    }
    
    - (void)cut:(UIMenuController *)menu
    {
        [self copy:menu];
        self.text = nil;
    }
    
    @end
    

    在实际的开发中,系统默认的菜单项就够了吗??
    肯定是不够的,我们往往要根据项目的实际需求去定制菜单项,比如:

    Paste_Image.png

    定制菜单项:

     UIMenuItem *item1 = [[UIMenuItem alloc] initWithTitle:@"item1" action:@selector(item1Click:)];
     UIMenuItem *item2 = [[UIMenuItem alloc] initWithTitle:@"item2" action:@selector(item2Click:)];
     menuVc.menuItems = @[item1,item2];
    
    // 处理菜单项的点击事件
    - (void)item1Click:(UIMenuController *)menu
    {
        NSLog(@"item1Click");
    }
    
    - (void)item2Click:(UIMenuController *)menu
    {
        NSLog(@"item2Click");
    }
    

    相关文章

      网友评论

        本文标题:iOS开发之-UIMenuController

        本文链接:https://www.haomeiwen.com/subject/mroflttx.html