2 qq 29574501 qq_29574501 于 2016.03.11 22:51 提问

iOS UIMenuController 在Label上添加自定义菜单栏,会与TextFile起冲突

代码:
#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的时候不显示自定义,只显示系统自带

1个回答

baidu_27566091
baidu_27566091   2016.03.14 10:44

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!