In iOS, you can use the Clipboard to implement data sharing among applications and between applications. For example, you can copy a URL from iphone qq and paste it into the Safari browser to see the content of the link.
One, the following three controls in iOS, there is a copy-paste function itself:
1, Uitextview
2, Uitextfield
3, UIWebView
The UIKit framework provides several classes and protocols to facilitate our ability to implement the Clipboard in our own applications.
1, Uipasteboard: We can write data to it, can also read the data
2. Uimenucontroller: Displays a shortcut menu for copying, clipping, and pasting selected items.
3. Canperformaction:withsender in Uiresponder: Used to control which commands are displayed in the shortcut menu.
4. Uiresponderstandardeditactions will be called when the command on the shortcut menu is clicked.
Three, the following items can be placed on the Clipboard
1, uipasteboardtypeliststring-string array, including Kuttypeutf8plaintext
2, Uipasteboardtypelisturl-url array, including Kuttypeurl
3. uipasteboardtypelistimage-graphic array, including Kuttypepng and Kuttypejpeg
4, uipasteboardtypelistcolor-color array
Four, the type of Clipboard is divided into two kinds:
System level: The system-level clipboard is created with Uipasteboardnamegeneral and Uipasteboardnamefind, and the data is not lost when the application is closed or unloaded.
Application-level: By setting, you can have the data remain on the Clipboard after the application is closed, but the data is lost after the application is unloaded. We can use Pasteboardwithname:create: to create.
Use of Uimenucontroller, copy of Uilabel and custom menus
1. The view of the menu must be implemented – (BOOL) Canbecomefirstresponder, and return Yes
2. The view of the menu must be implemented – (BOOL) Canperformaction:withsender and return Yes or no as required
3. Make menu View a first Responder (Becomefirstresponder)
4. Position menu (-Settargetrect:inview:)
5. Display Menu (-setmenuvisible:animated:)
- @implementation Uicopylabel
- Default is NO
- -(BOOL) canbecomefirstresponder{
- return YES;
- }
- Features of "Feedback" care
- -(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
- return (Action = = @selector (copy:));
- }
- Implementation for copy
- -(void) copy: (ID) sender{
- Uipasteboard *pboard = [Uipasteboard Generalpasteboard];
- pboard.string = Self.text;
- }
- Uilabel The default is not to receive events, we need to add the touch event ourselves
- -(void) attachtaphandler{
- self.userinteractionenabled = YES; //master switch for user interaction
- UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Handletap:)];
- touch.numberoftapsrequired = 2;
- [Self addgesturerecognizer:touch];
- [Touch release];
- }
- Binding events
- -(ID) initWithFrame: (CGRect) frame
- {
- self = [super Initwithframe:frame];
- if (self) {
- [Self attachtaphandler];
- }
- return self;
- }
- Ditto
- -(void) awakefromnib{
- [Super awakefromnib];
- [Self attachtaphandler];
- }
- -(void) Handletap: (uigesturerecognizer*) recognizer{
- [Self becomefirstresponder];
- Uimenucontroller *menu = [Uimenucontroller Sharedmenucontroller];
- [Menu SetTargetRect:self.frame InView:self.superview];
- [Menu Setmenuvisible:yes Animated:yes];
- }
- @end
Add a Uicopylabel to the view
You can now use Uicopylabel to implement a double-click to copy the contents of the label
In your view
Uicopylabel *display = [[uicopylabelalloc]initwithframe: CGRectMake(100 , (+ )];
Awakefromnib
The use of this method is only involved when using IB, When the. nib file is loaded, a awakefromnib message is sent to each object in the. nib file, and each object can define its own awakefromnib function to respond to the message and perform some necessary actions.
See Example:
Create a Viewcontroller with XIB
Define a subclass of UIView
Open Xib and specify the type of view as the subclass defined in the previous step
Then add the awakefromnib method in TESTVIEW.M, run the program to find this method is called!!!
Now let's customize the menu.
add a long push gesture to the Attachtaphandler
- -(void) attachtaphandler{
- self.userinteractionenabled = YES; //master switch for user interaction
- //double-click
- UITapGestureRecognizer *touch = [[UITapGestureRecognizer alloc] initwithtarget:self action: @selector (Handletap:)];
- touch.numberoftapsrequired = 2;
- [Self addgesturerecognizer:touch];
- [Touch release];
- //Long Press
- Uilongpressgesturerecognizer *press = [[Uilongpressgesturerecognizer alloc]initwithtarget:self Action: @selector ( Longpress:)];
- Press.minimumpressduration = 1.0;
- [Self addgesturerecognizer:press];
- [Press release];
- }
Add Method longpress
- -(void) longpress: (Uilongpressgesturerecognizer *) recognizer {
- if (recognizer.state = = Uigesturerecognizerstatebegan) {
- Tstableviewcell *cell = (Tstableviewcell *) Recognizer.view;
- [Self becomefirstresponder];
- UIMenuItem *flag = [[UIMenuItem alloc] initwithtitle:@"flag" Action: @selector (flag:)];
- UIMenuItem *approve = [[UIMenuItem alloc] initwithtitle:@"approve" Action: @selector (approve:)];
- UIMenuItem *deny = [[UIMenuItem alloc] initwithtitle:@"Deny" Action: @selector (deny:)];
- Uimenucontroller *menu = [Uimenucontroller Sharedmenucontroller];
- [Menu Setmenuitems:[nsarray Arraywithobjects:flag, approve, deny, Nil]];
- [Menu SetTargetRect:self.frame InView:self.superview];
- [Menu Setmenuvisible:yes Animated:yes];
- NSLog (@"menuitems:%@", Menu.menuitems);
- }
- }
- -(void) flag: (ID) Sender {
- NSLog (@"Cell was flagged");
- }
- -(void) Approve: (ID) Sender {
- NSLog (@"Cell was approved");
- }
- -(void) deny: (ID) Sender {
- NSLog (@"Cell was denied");
- }
Modify Canperformaction
[CPP]View Plaincopyprint?
- -(BOOL) Canperformaction: (SEL) Action Withsender: (ID) sender{
- return (action = = @selector (copy:));
- if (action = = @selector (copy:) | | Action = = @selector (flag:) | | Action = = @selector (approve:) | | Action = = @selector (deny:)) {
- return YES;
- }
- }
Ok... Effect
Use of the iOS copy sticker