菜单事件包括,剪切、拷贝、全选、分享...,此 demo 只有 copy、share
1.定义 field 继承与 UITextField
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender { if (action == @selector(copy:)) { return YES; } return NO; } - (void)copy:(id)sender { [self resignFirstResponder]; //持久化,退出程序 copy 的内容还在 [[UIPasteboard generalPasteboard]setPersistent:YES]; //拷贝的内容 [[UIPasteboard generalPasteboard]setValue:self.text forPasteboardType:[UIPasteboardTypeListString objectAtIndex:0]]; }
2.ViewController 加载
field = [[CCField alloc]initWithFrame:CGRectMake(100, 200, 100, 40)]; field.backgroundColor = [UIColor greenColor]; [self.view addSubview:field]; UIMenuItem *shareItem = [[UIMenuItem alloc]initWithTitle:@"分享" action:@selector(click)]; UIMenuController *menu = [UIMenuController sharedMenuController]; [menu setMenuItems:[NSArray arrayWithObjects:shareItem, nil]];
3.分享的实现,以微信为例,准备工作在这里
SendMessageToWXReq *sendReq = [[SendMessageToWXReq alloc]init]; // 使用文本信息 sendReq.bText = YES; // 发到还有列表 , 默认是 0 sendReq.scene = 0; sendReq.text = field.text; //发送分享信息 [WXApi sendReq:sendReq]; NSLog(@"213");
完成demo 在 githud,点我查看