Xing_1102 2016-03-11 14:51 采纳率: 35.5%
浏览 2082

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条回答

  • 天材地宝 2016-03-14 02:44
    关注

    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?