UIMenuController
2018-03-06 本文已影响10人
Luyc_Han
#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