1. Copy-paste function 2.UIMenuController use 3. Specific code 4 effect 5. Code Demo Example
1. Copy-paste Function
The following three controls in iOS have their own copy-paste functionality: Uitextview Uitextfield UIWebView
The Uikit framework provides several classes and protocols that enable us to implement the Clipboard functionality in our own applications. Uipasteboard: The pasteboard is a protected area that is used for data sharing within an application or between different applications. Uimenucontroller: Displays a shortcut menu to show items such as copy, clip, paste, and so on. Canperformaction:withsender in Uiresponder: Controls which commands are displayed in the shortcut menu. Use of 2.UIMenuController 1. Make menu The view become the responder (Becomefirstresponder)
The view or Viewcontroller of the menu must be implemented-(BOOL) Canbecomefirstresponder, and return yes so that the view or Viewcontroller self becomes the first responder, Otherwise the Canperformaction:withsender method will not go
[Self becomefirstresponder];
2.Menu view must implement-(BOOL) Canperformaction:withsender and return yes or no on demand
Overloaded function-(BOOL) Canperfomaction: (SEL) Action Withsender: (ID) sender, set the menu item to display, and the return value is yes. Without any restrictions, all menu items with the system will be displayed L 3. Position menu (-Settargetrect:inview:) 4. Show menu (-setmenuvisible:animated:)
3. Specific Code
ZQCHATFOOTBALLVIEWCELL.M//Zqmenucontroller////Created by Zhouyu on 10/04/2018. copyright©2018 Zhouyu.
All rights reserved. #import "ZQChatFootballViewCell.h" @interface Zqchatfootballviewcell () @property (nonatomic, strong) Uilabel *conten
Tlabel; @end @implementation Zqchatfootballviewcell-(Instancetype) Initwithstyle: (Uitableviewcellstyle) style Reuseidentifier: (NSString *) Reuseidentifier {if (self = [super Initwithstyle:style Reuseidentifier:reuseidentifier])
{Self.selectionstyle = Uitableviewcellselectionstylenone;
_contentlabel = [[Uilabel alloc] Initwithframe:cgrectmake (100, 200, 200, 100)]; _contentlabel.text = @ "This is a text that automatically hides the copy and Paste menu when the system finds that there are touch events in other view. In addition, when the Alarm dialog box pops up or the software exits, the Copy button is also hidden.
Of course, or through ";
_contentlabel.textalignment = Nstextalignmentleft;
_contentlabel.numberoflines = 0;
_contentlabel.userinteractionenabled = YES; [_contentlabel Addgesturerecognizer:[[uilongpressgesturerecognizer ALloc] initwithtarget:self action: @selector (longpress:)]];
[Self.contentview Addsubview:_contentlabel];
Menu's view must be implemented-otherwise canperformaction: (SEL) Action Withsender: (ID) Sender method does not go [self becomefirstresponder];
return self;
}-(void) layoutsubviews {[Super layoutsubviews];
_contentlabel.frame = Self.contentView.bounds; #pragma mark-longpress-(void) Longpress: (Uilongpressgesturerecognizer *) gesture {Uimenucontroller * menu = [UIM
Enucontroller Sharedmenucontroller];
UIMenuItem * item1 = [[UIMenuItem alloc] initwithtitle:@ "copy" Action: @selector (mycopy:)];
UIMenuItem * item2 = [[UIMenuItem alloc] initwithtitle:@ "Report" Action: @selector (mywhistleblowing:)];
Menu.menuitems = @[item1,item2]; if (menu.ismenuvisible) return;//Avoid repeated display [menu Settargetrect:cgrectmake (Cgrectgetwidth (_contentlabel.frame)/2, 15, 0, 0) inview:self.contentview];//positioning [Menu Setmenuvisible:yes animated:yes];//display} #pragma mark-private//menu where the VIew must be implemented-(BOOL) Canbecomefirstresponder {return YES} Menu's view must be implemented-(BOOL) Canperformaction: (SEL) Action Withsender: (ID) Sender {if (action = = @selector (mycopy:) | | acti
on = = @selector (mywhistleblowing:)) {return YES;
else {return NO;
}} #pragma mark-event-(void) Mycopy: (Uimenucontroller *) menu{if (!_contentlabel.text) return;
Uipasteboard * paste = [Uipasteboard Generalpasteboard];
paste.string = _contentlabel.text; }-(void) mywhistleblowing: (Uimenucontroller *) menu{if (!_contentlabel.text) return;//uipasteboard * paste = [U
Ipasteboard Generalpasteboard];
paste.string = _contentlabel.text;
_contentlabel.text = nil; if (self.delegate && [self.delegate respondstoselector: @selector (whistleblowingwithcontent:)]) {[Self.del
Egate Whistleblowingwithcontent:_contentlabel.text];
}} @end
4. Effect
5. Code Demo Example
The use and considerations of the copy and paste API first of all to understand is uimenucontroller, that is, copy and Paste menu Controller, it is used to control the use of copy paste when the pop-up button. This controller has only one instance of the entire system, and it should be shown which button is to be determined by it. The copy-and-paste menu is automatically hidden when the system finds a touch event in another view. In addition, when the Alarm dialog box pops up or the software exits, the Copy button is also hidden. Of course, you can hide it either by-setmenuvisible:animated: method, or by assigning it to uimenucontroller.menuvisible, or by manually hiding it.
Demo