菜單事件包括,剪切、拷貝、全選、分享...,此 demo 只有 copy、share 1.定義 field 繼承與 UITextField 2.ViewController 載入 3.分享的實現,以微信為例,準備工作在這裡 完成demo 在 githud,點我查看 ...
菜單事件包括,剪切、拷貝、全選、分享...,此 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,點我查看