IOS study notes 9-UIPasteboard clipboard

Source: Internet
Author: User

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:


// "Feedback" functions of interest
-(BOOL) can1_maction :( SEL) action withSender :( id) sender {
Return (action = @ selector (copy :));
}
// For copy 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 touch events by ourselves.
-(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 events
-(Id) initWithFrame :( CGRect) frame
{
Self = [super initWithFrame: frame];
If (self ){
[Self attachTapHandler];
}
Return self;
}
// Same as above
-(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];
UIMenuController * menu = [UIMenuController sharedMenuController];
[Menu setTargetRect: self. frame inView: self. superview];
[Menu setMenuVisible: YES animated: 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.

----------------------------------------- The boundary -----------------------------------------

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) can1_maction :( 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;
}
----------------------------------------- The boundary -----------------------------------------
UIPasteboard can not only communicate in the application, but also communicate between applications, you should have seen it, for example, I copy a url, then open safari, paste it to the address bar, however, we can "quietly" communicate and share data between applications.


Note: here the "quiet" only means that other applications do not know, and the system is allowed.
 


We use the template Single View Application to create two simple projects: PasteBoardWrite and PasteBoardRead. The interfaces are as follows:


 


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
UIPasteboard * pb = [UIPasteboard pasteboardWithName: @ "testBoard" create: YES];
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 can be converted to plist data types (NSString, NSArray, NSDictionary, NSDate, NSNumber, and NSURL), we can call setValue: forPasteboardType: Method to store data, other types can only call setData: forPasteboardType: method (plist data type can also be used), similar to this:


NSDictionary * dict = [NSDictionary dictionaryWithObject: textField. text forKey: @ "content"];
NSData * dictData = [NSKeyedArchiver archivedDataWithRootObject: dict];
[Pb setData: dictData forPasteboardType: @ "myType"];
The result is similar to the following:


UIPasteboard * pb = [UIPasteboard pasteboardWithName: @ "testBoard" create: YES];
NSDictionary * dict = [NSKeyedUnarchiver unarchiveObjectWithData: [pb dataForPasteboardType: @ "myType"];
Caption. text = [dict objectForKey: @ "content"];


As mentioned above, a PasteboardType 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.

With your own clipboard, you can only communicate with your local applications. Sometimes it can make your user experience better, for example.

 

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.