代码:
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)UILabel *lblTitle;
@property(nonatomic,strong)UITextField *txtTitle;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.lblTitle=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,200, 30)];
self.lblTitle.center=self.view.center;
self.lblTitle.userInteractionEnabled=YES;
self.lblTitle.textAlignment=NSTextAlignmentCenter;
self.lblTitle.text=@"156165156161651";
[self.view addSubview:self.lblTitle];
UILongPressGestureRecognizer *longPress=[[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
[self.lblTitle addGestureRecognizer:longPress];
self.txtTitle=[[UITextField alloc]initWithFrame:CGRectMake(0, 0, 200, 30)];
self.txtTitle.center=CGPointMake(self.view.center.x, self.lblTitle.center.y+50);
self.txtTitle.borderStyle=UITextBorderStyleRoundedRect;
[self.view addSubview:self.txtTitle];
}
-(void)longPressAction:(UILongPressGestureRecognizer *)sender
{
if (sender.state==UIGestureRecognizerStateEnded)
{
UIMenuItem *copyItem=[[UIMenuItem alloc]initWithTitle:@"复制" action:@selector(copyAction:)];
UIMenuController *menuC=[UIMenuController sharedMenuController];
[menuC setMenuItems:@[copyItem]];
[menuC setTargetRect:sender.view.frame inView:self.view];
[menuC setMenuVisible:YES animated:YES];
}
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action==@selector(copyAction:))
{
return YES;
}
return NO;
}
-(void)copyAction:(UIMenuItem *)sender
{
UIPasteboard *pasteBoard=[UIPasteboard generalPasteboard];
pasteBoard.string=self.lblTitle.text;
NSLog(@"%@",pasteBoard.string);
}
就是在txtfile的时候不显示自定义,只显示系统自带