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
    关注

    图片说明

    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?