Basic knowledge about IOS clipboard

Source: Internet
Author: User

In iOS, the following three controls have the copy-and-paste function:

1. uitextview

2. uitextfield

3. uiwebview


The uikit framework provides several classes and protocols for us to implement the clipboard function in our own applications.

1. uipasteboard: You can write data to or read data.
2. uimenucontroller: displays a shortcut menu to show the items selected, such as copy, clipboard, and paste.
3. can1_maction: withsender in uiresponder: used to control which commands are displayed in the shortcut menu.
4. When you click a command on the shortcut menu, uiresponderstandardeditactions will be called.

The following items can be placed in the clipboard.

1. uipasteboardtypeliststring-string array, which contains the kuttypeutf8plaintext
2. uipasteboardtypelisturl-URL array, including
3. uipasteboardtypelistimage-Image array, including the kuttypepng and kuttypejpeg
4. uipasteboardtypelistcolor-color array

There are two types of clipboard:
System Level: uipasteboardnamegeneral and uipasteboardnamefind are used. system level applications are disabled or unmounted data is not lost.

Application-level: by setting, data can be stored in the clipboard after the application is closed, but the data will be lost after the application is uninstalled. We can use pasteboardwithname: Create: To create an instance.

Example:

Sometimes we may need to copy the text on uilabel or the image of uiimageview. By default, uilabel and uiimageview do not respond to the touch event and cannot be copied, then we need to implement a replicable uilabel. Add a new class inherited from uilabel:

@interface UICopyLabel : UILabel  @end  #import "UICopyLabel.h"  @implementation UICopyLabel  @end  

In order to receive the event (to be the first responder), we need to overwrite a method:

-(BOOL)canBecomeFirstResponder{      return YES;  }  

There are also two methods for copying:

// Response method-(bool) canperformaction :( SEL) Action withsender :( ID) sender {return (Action ==@ selector (copy :));}

// Response method implementation-(void) Copy :( ID) sender {uipasteboard * pboard = [uipasteboard generalpasteboard]; pboard. String = self. Text ;}

With the above three methods, we can process the copy. Of course, when we can receive the event:

// Uilabel does not receive events by default. We need to add the touch event-(void) attachtaphandler {self. userinteractionenabled = yes; // user interaction switch uitapgesturerecognizer * Touch = [[uitapgesturerecognizer alloc] initwithtarget: Self action: @ selector (handletap :)]; touch. numberoftapsrequired = 2; [self addgesturerecognizer: Touch]; [Touch release];} // bind event-(ID) initwithframe :( cgrect) frame {self = [Super initwithframe: frame]; if (Self) {[self attachtaphandler];} return self;} // same as-(void) awakefromnib {[Super awakefromnib]; [self attachtaphandler];}

We can now receive events! Since we set the number of taps to 2 in the above, we need to double-click the number to capture. Next, we need to process the tap so that the menu bar can pop up:

-(Void) handletap :( uigesturerecognizer *) recognizer {[self becomefirstresponder]; uimenuitem * copylink = [[[uimenuitemalloc] initwithtitle: @ "copy" action: @ selector (copy :)] autorelease]; [[uimenucontrollersharedmenucontroller] setmenuitems: [nsarrayarraywithobjects: copylink, nil]; [Response] settargetrect: Self. frameinview: Self. superview]; [[uimenucontrollersharedmenucontroller] setmenuvisible: yesanimated: Yes];}

In this way, a replicable uilabel is born! It can process received clicks, pop-up menu bar, and process copy, which is a common reproducible control.

Next we will create a reproducible uiimageview, create a new viewcontroller, and put two imageviews. By default, different images are displayed:
Then copy the above Code and change it to three places:

-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{      return (action == @selector(copy:) || action == @selector(paste:));  }    -(void)copy:(id)sender{      UIPasteboard *pboard = [UIPasteboard generalPasteboard];      pboard.image = self.image;  }    -(void)paste:(id)sender{      UIPasteboard *pboard = [UIPasteboard generalPasteboard];      self.image = pboard.image;  }  

Uipasteboard has two types: system-level and application-level. Therefore, you can not only communicate within the application, but also between applications. For example, if I copy a URL and open safari, paste it to the address bar, and we can communicate and share data between applications.

Click "write" in pasteboardwrite, write the text in textfield to the clipboard, and display it when switching to pasteboardread. If we only want to use the clipboard for our own purposes, we cannot use the general clipboard of the system. We need to create one ourselves:

// A unique name is required. Generally, the inverted domain name is com. mycompany. myApp. pboard // The following parameter indicates whether to create a uipasteboard * pb = [uipasteboard pasteboardwithname: @ "testboard" create: Yes] if it does not exist.

With this clipboard, we can save the text and read it in another app. Some common types have been set to attributes:


In addition, if it is a data type that can be converted to plist (nsstring, nsarray, nsdictionary, nsdate, nsnumber, and nsurl), we can callSetvalue: forpasteboardtype:Method to store data, other types can only be calledSetdata: forpasteboardtype:Method (plist data type can also be used), similar to the following:

// Store data nsdictionary * dict = [nsdictionary dictionarywithobject: textfield. text forkey: @ "content"]; nsdata * dictdata = [nskeyedarchiver archiveddatawithrootobject: dict]; [Pb setdata: dictdata forpasteboardtype: @ "mytype"]; // The result is similar: uipasteboard * pb = [uipasteboard pasteboardwithname: @ "testboard" create: Yes]; nsdictionary * dict = [pipeline metadata: [Pb dataforpasteboardtype: @ "mytype"]; caption. TEXT = [dict objectforkey: @ "content"];

As mentioned abovePasteboardtypeThis is a unified type identifier (Uniform type identifier UTI), which can help the app obtain data that it can process. For example, if you can only process text pasting, it is useless to give you a uiimage. You can use the common UTI or any character. We recommend that you use the inverted domain name and the type name com. mycompany. MyApp. mytype.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.