美文网首页
UIMenuController

UIMenuController

作者: Luyc_Han | 来源:发表于2018-03-06 11:13 被阅读10次
    #import <UIKit/UIKit.h>
    
    @interface GJMenuLabel : UILabel
    
    /// 调用弹出
    - (void)showMenuController;
    
    @end
    
    
    #import "GJMenuLabel.h"
    
    @implementation GJMenuLabel
    
    - (void)awakeFromNib
    {
        [super awakeFromNib];
        [self setup];
    }
    
    - (instancetype)initWithFrame:(CGRect)frame
    {
        if (self = [super initWithFrame:frame]) {
            [self setup];
        }
        return self;
    }
    
    - (void)setup
    {
        self.userInteractionEnabled = YES;
    }
    
    - (BOOL)canBecomeFirstResponder
    {
        return YES;
    }
    
    - (BOOL)canPerformAction:(SEL)action withSender:(id)sender
    {
        if (action == @selector(copys:)) return YES;
        
        return NO;
    }
    
    - (void)showMenuController {
        
        [self becomeFirstResponder];
        
        UIMenuController *menu = [UIMenuController sharedMenuController];
        
        UIMenuItem *ding = [[UIMenuItem alloc] initWithTitle:@"复制" action:@selector(copys:)];
        
        [menu setMenuItems:[NSArray arrayWithObjects:ding, nil]];
        
        [menu setTargetRect:self.bounds inView:self];
        
        [menu setMenuVisible:YES animated:YES];
        
    }
    
    - (void)copys:(UIMenuController *)menu
    {
        // 将自己的文字复制到粘贴板
        UIPasteboard *board = [UIPasteboard generalPasteboard];
        board.string = self.text;
    }
    
    @end
    
    

    相关文章

      网友评论

          本文标题:UIMenuController

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