Uipasteboard Paste Board Class usage in IOS management clipboard _ios

Source: Internet
Author: User


First, native UI controls with the clipboard operation
in the iOS UI system, there are 3 controls that have their own clipboard operations, namely Uitextfield, Uitextview, and UIWebView. A long press gesture at the text interaction of these controls allows the system's clipboard control to be called on the screen view, and the user can copy, paste, cut, and so on, and the effect is shown in the following illustration.






Uitextfield Text Operations






Uitextview text Operations









Second, the system of the cutting board management class Uipasteboard



In fact, when the user through the above space for copying, cutting and other operations, the selected content will be stored in the system's Clipboard, and the Clipboard is not only the string data, it can also carry out picture data and URL data storage. The Clipboard is the Uipasteboard class, and developers can also directly manipulate the data for application or transfer between applications.



The Uipasteboard class has 3 initialization methods, as follows:

// Get the system-level clipboard
+ (UIPasteboard *) generalPasteboard;
// Get a custom clipboard name parameter is the name of the clipboard create parameter is used to set whether to create when this clipboard does not exist
+ (nullable UIPasteboard *) pasteboardWithName: (NSString *) pasteboardName create: (BOOL) create;
// Get a clipboard available in the app
+ (UIPasteboard *) pasteboardWithUniqueName;
The above three initialization methods respectively obtain or create three different levels of clipboards. The system-level clipboard is shared among the entire device, that is, the application is deleted and it is written to the system-level clipboard Of data is still there. The custom clipboard is created by a specific name string, which can be used for data sharing within the application or other applications developed by the same developer. The clipboard created by the third method is equivalent to the clipboard created by the second method, but its name string is nil, which is usually used inside the current application.

Note: The clipboard created by the third method does not persist data by default, and when the application exits, the contents of the clipboard will not be erased. To achieve persistence, you need to set the persistent property to YES.

Common methods and properties in UIPasteboard are as follows:

// The name of the clipboard
@property (readonly, nonatomic) NSString * name;
// Delete a clipboard based on the name
+ (void) removePasteboardWithName: (NSString *) pasteboardName;
// Whether to persist
@property (getter = isPersistent, nonatomic) BOOL persistent;
// The number of changes to this clipboard. The system-level clipboard will only be cleared when the device is restarted.
@property (readonly, nonatomic) NSInteger changeCount;
The following methods are used to set and get the data in the clipboard:

Access to the latest set of data objects:

// Get the latest data type in the clipboard
-(NSArray <NSString *> *) pasteboardTypes;
// Get whether the latest data object in the clipboard contains a certain type of data
-(BOOL) containsPasteboardTypes: (NSArray <NSString *> *) pasteboardTypes;
// Remove a certain type of data from the latest data object in the clipboard
-(nullable NSData *) dataForPasteboardType: (NSString *) pasteboardType;
// Retrieve a value of a certain type from the latest data object in the clipboard
-(nullable id) valueForPasteboardType: (NSString *) pasteboardType;
// Set the value for a data type corresponding to the latest data in the clipboard
-(void) setValue: (id) value forPasteboardType: (NSString *) pasteboardType;
// Set data for a data type corresponding to the latest data in the clipboard
-(void) setData: (NSData *) data forPasteboardType: (NSString *) pasteboardType;
Access to multiple sets of data objects:

// Number of data groups
@property (readonly, nonatomic) NSInteger numberOfItems;
// Get the data type contained in a set of data objects
-(nullable NSArray *) pasteboardTypesForItemSet: (nullable NSIndexSet *) itemSet;
// Get whether a set of data objects contain certain data types
-(BOOL) containsPasteboardTypes: (NSArray <NSString *> *) pasteboardTypes inItemSet: (nullable NSIndexSet *) itemSet;
// Acquire a set of data objects according to the data type
-(nullable NSIndexSet *) itemSetWithPasteboardTypes: (NSArray *) pasteboardTypes;
// Acquire the value of a set of data according to the data type
-(nullable NSArray *) valuesForPasteboardType: (NSString *) pasteboardType inItemSet: (nullable NSIndexSet *) itemSet;
// Acquire the NSData data of a group of data according to the data type
-(nullable NSArray *) dataForPasteboardType: (NSString *) pasteboardType inItemSet: (nullable NSIndexSet *) itemSet;
// All data objects
@property (nonatomic, copy) NSArray * items;
// Add a set of data objects
-(void) addItems: (NSArray <NSDictionary <NSString *, id> *> *) items;

Many of the above methods need to pass in data type parameters. These parameters are defined by the system as follows:

// The type definition string array of all string type data
UIKIT_EXTERN NSArray <NSString *> * UIPasteboardTypeListString;
// The type definition string array of all URL type data
UIKIT_EXTERN NSArray <NSString *> * UIPasteboardTypeListURL;
// All image data types define string data
UIKIT_EXTERN NSArray <NSString *> * UIPasteboardTypeListImage;
// All color data types define string array
UIKIT_EXTERN NSArray <NSString *> * UIPasteboardTypeListColor;


Compared with the above two methods, the following methods are more object-oriented and more convenient and faster to use in development:

// Get or set the string data in the clipboard
@property (nullable, nonatomic, copy) NSString * string;
// Get or set the string array in the clipboard
@property (nullable, nonatomic, copy) NSArray <NSString *> * strings;
// Get or set the URL data in the clipboard
@property (nullable, nonatomic, copy) NSURL * URL;
// Get or set the URL array in the clipboard
@property (nullable, nonatomic, copy) NSArray <NSURL *> * URLs;
// Get or s more than the picture data in the clipboard
@property (nullable, nonatomic, copy) UIImage * image;
// Get or set the picture array in the clipboard
@property (nullable, nonatomic, copy) NSArray <UIImage *> * images;
// Get or set the color data in the clipboard
@property (nullable, nonatomic, copy) UIColor * color;
// Get or set the color array in the clipboard
@property (nullable, nonatomic, copy) NSArray <UIColor *> * colors;
Some operations on the clipboard will trigger the following notifications:

// Notification sent when the content of the clipboard changes
UIKIT_EXTERN NSString * const UIPasteboardChangedNotification;
// Notification sent when the key value of the clipboard data type increases
UIKIT_EXTERN NSString * const UIPasteboardChangedTypesAddedKey;
// Notification sent when key value of clipboard data type is removed
UIKIT_EXTERN NSString * const UIPasteboardChangedTypesRemovedKey;
// Notification sent when the clipboard is deleted
UIKIT_EXTERN NSString * const UIPasteboardRemovedNotification;

Third, a simple example of copying pictures
Create a CopyView

#import "CopyView.h"
@interface CopyView ()
@property (strong, nonatomic) UIImageView * img1;
@property (strong, nonatomic) UIImageView * img2;
@end

@implementation CopyView
-(UIImageView *) img1 {
  if (_img1 == nil) {
    _img1 = [[UIImageView alloc] initWithFrame: CGRectMake (10.0f, 20.0f, 100.0f, 100.0f)];
    NSString * path = [[NSBundle mainBundle] pathForResource: @ "NetworldImage" ofType: @ "jpg"];
    _img1.image = [UIImage imageWithContentsOfFile: path];
  }
  return _img1;
}

-(UIImageView *) img2 {
  if (_img2 == nil) {
     _img2 = [[UIImageView alloc] initWithFrame: CGRectMake (CGRectGetMaxX (self.img1.frame) + 50.0f, 20.0f, 100.0f, 100.0f)];
    _img2.backgroundColor = [UIColor lightGrayColor];
  }
  return _img2;
}

-(instancetype) initWithFrame: (CGRect) frame {
  self = [super initWithFrame: frame];
  if (self) {
    self.backgroundColor = [UIColor whiteColor];
    [self addSubview: self.img1];
    [self addSubview: self.img2];
  }
  return self;
}

-(BOOL) canBecomeFirstResponder {
  return YES;
}
-(BOOL) canPerformAction: (SEL) action withSender: (id) sender {
  NSArray * methodNameArr = @ [@ "copy:", @ "cut:", @ "select:", @ "selectAll:", @ "paste:"];
  if ([methodNameArr containsObject: NSStringFromSelector (action)]) {
    return YES;
  }
  return [super canPerformAction: action withSender: sender];
}

-(void) copy: (id) sender {
  UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];
  [pasteboard setImage: self.img1.image];
}

-(void) paste: (id) sender {
   UIPasteboard * pasteboard = [UIPasteboard generalPasteboard];
   self.img2.image = [pasteboard image];
}

-(void) touchesBegan: (NSSet <UITouch *> *) touches withEvent: (UIEvent *) event {
   [self becomeFirstResponder];
   UIMenuController * menuController = [UIMenuController sharedMenuController];
   [menuController setTargetRect: self.img1.frame inView: self];
   [menuController setMenuVisible: YES animated: YES];
}

@end
In the controller

#import "ViewController.h"
#import "CopyView.h"

@implementation ViewController

-(void) viewDidLoad {
   [super viewDidLoad];
   CopyView * cv = [[CopyView alloc] initWithFrame: self.view.bounds];
   self.view = cv;
}

@end

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.