在iOS中,可以使用剪貼板實現應用程序之中以及應用程序之間實現數據的共享。比如你可以從iPhone QQ復制一個url,然后粘貼到safari瀏覽器中查看這個鏈接的內容。
一、在iOS中下面三個控件,自身就有復制-粘貼的功能:
1、UITextView
2、UITextField
3、UIWebView
二、UIKit framework提供了幾個類和協議方便我們在自己的應用程序中實現剪貼板的功能。
1、UIPasteboard:我們可以向其中寫入數據,也可以讀取數據
2、UIMenuController:顯示一個快捷菜單,用來復制、剪貼、粘貼選擇的項。
3、UIResponder中的 canPerformAction:withSender:用於控制哪些命令顯示在快捷菜單中。
4、當快捷菜單上的命令點擊的時候,UIResponderStandardEditActions將會被調用。
三、下面這些項能被放置到剪貼板中
1、UIPasteboardTypeListString — 字符串數組, 包含kUTTypeUTF8PlainText
2、UIPasteboardTypeListURL — URL數組,包含kUTTypeURL
3、UIPasteboardTypeListImage — 圖形數組, 包含kUTTypePNG 和kUTTypeJPEG
4、UIPasteboardTypeListColor — 顏色數組
四、剪貼板的類型分為兩種:
系統級:使用UIPasteboardNameGeneral和UIPasteboardNameFind創建,系統級的剪貼板,當應用程序關閉,或者卸載時,數據都不會丟失。
應用程序級:通過設置,可以讓數據在應用程序關閉之后仍然保存在剪貼板中,但是應用程序卸載之后數據就會失去。我們可用通過pasteboardWithName:create:來創建。
了解這些之后,下面通過一系列的例子來說明如何在應用程序中使用剪貼板
例子:
1、復制剪貼文本。
下面通過一個例子,可以在tableview上顯示一個快捷菜單,上面只有復制按鈕,復制tableview上的數據之后,然后粘貼到title上。
定義一個單元格類CopyTableViewCell,在這個類的上顯示快捷菜單,實現復制功能。
- @interface CopyTableViewCell : UITableViewCell {
- id delegate;
- }
- @property (nonatomic, retain) id delegate;
- @end
@interface CopyTableViewCell : UITableViewCell { id delegate; } @property (nonatomic, retain) id delegate; @end
//實現CopyTableViewCell :
- #import "CopyTableViewCell.h"
- @implementation CopyTableViewCell
- @synthesize delegate;
- - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
- if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { }
- return self;
- }
- - (void)setSelected:(BOOL)selected animated:(BOOL)animated {
- [super setSelected:selected animated:animated]; }
- - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {
- [[self delegate] performSelector:@selector(showMenu:) withObject:self afterDelay:0.9f];
- [super setHighlighted:highlighted animated:animated];
- }
- - (BOOL)canBecomeFirstResponder {
- return YES;
- }
- - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
- if (action == @selector(cut:)){
- return NO;
- }else if(action == @selector(copy:)){
- return YES;
- } else if(action == @selector(paste:)){
- return NO;
- } else if(action == @selector(select:)){
- return NO;
- } else if(action == @selector(selectAll:)){
- return NO;
- } else {
- return [super canPerformAction:action withSender:sender];
- }
- }
- - (void)copy:(id)sender {
- UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
- [pasteboard setString:[[self textLabel]text]]; }
- - (void)dealloc {
- [super dealloc];
- }
- @end
- //定義CopyPasteTextController,實現粘貼功能。
- @interface CopyPasteTextController : UIViewController<UITableViewDelegate> {
- //用來標識是否顯示快捷菜單
- BOOL menuVisible;
- UITableView *tableView;
- }
- @property (nonatomic, getter=isMenuVisible) BOOL menuVisible;
- @property (nonatomic, retain) IBOutlet UITableView *tableView;
- @end
#import "CopyTableViewCell.h" @implementation CopyTableViewCell @synthesize delegate; - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier { if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) { } return self; } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; } - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated { [[self delegate] performSelector:@selector(showMenu:) withObject:self afterDelay:0.9f]; [super setHighlighted:highlighted animated:animated]; } - (BOOL)canBecomeFirstResponder { return YES; } - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{ if (action == @selector(cut:)){ return NO; }else if(action == @selector(copy:)){ return YES; } else if(action == @selector(paste:)){ return NO; } else if(action == @selector(select:)){ return NO; } else if(action == @selector(selectAll:)){ return NO; } else { return [super canPerformAction:action withSender:sender]; } } - (void)copy:(id)sender { UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; [pasteboard setString:[[self textLabel]text]]; } - (void)dealloc { [super dealloc]; } @end //定義CopyPasteTextController,實現粘貼功能。 @interface CopyPasteTextController : UIViewController<UITableViewDelegate> { //用來標識是否顯示快捷菜單 BOOL menuVisible; UITableView *tableView; } @property (nonatomic, getter=isMenuVisible) BOOL menuVisible; @property (nonatomic, retain) IBOutlet UITableView *tableView; @end
實現CopyPasteTextController :
- #import "CopyPasteTextController.h"
- #import "CopyTableViewCell.h"
- @implementation CopyPasteTextController
- @synthesize menuVisible,tableView;
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self setTitle:@"文字復制粘貼"];
- //點擊這個按鈕將剪貼板的內容粘貼到title上
- UIBarButtonItem *addButton = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self
- action:@selector(readFromPasteboard:)]autorelease];
- [[self navigationItem] setRightBarButtonItem:addButton];
- }
- // Customize the number of sections in the table view.
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
- return 1;
- }
- - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
- return 9;
- }
- // Customize the appearance of table view cells.
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
- static NSString *CellIdentifier =@"Cell";
- CopyTableViewCell *cell = (CopyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil) {
- cell = [[[CopyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
- [cell setDelegate:self];
- }
- // Configure the cell.
- NSString *text = [NSString stringWithFormat:@"Row %d", [indexPath row]];
- [[cell textLabel] setText:text];
- return cell;
- }
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
- if([self isMenuVisible]) {
- return;
- }
- [[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES animated:YES];
- }
- //顯示菜單
- - (void)showMenu:(id)cell {
- if ([cell isHighlighted]) {
- [cell becomeFirstResponder];
- UIMenuController * menu = [UIMenuController sharedMenuController];
- [menu setTargetRect: [cell frame] inView: [self view]];
- [menu setMenuVisible: YES animated: YES];
- }
- }
- - (void)readFromPasteboard:(id)sender {
- [self setTitle:[NSString stringWithFormat:@"Pasteboard = %@",
- [[UIPasteboard generalPasteboard] string]]];
- }
- - (void)didReceiveMemoryWarning {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Relinquish ownership any cached data, images, etc that aren't in use.
- }
- - (void)viewDidUnload {
- [super viewDidUnload];
- [self.tableView release];
- // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
- // For example: self.myOutlet = nil;
- }
#import "CopyPasteTextController.h" #import "CopyTableViewCell.h" @implementation CopyPasteTextController @synthesize menuVisible,tableView; - (void)viewDidLoad { [super viewDidLoad]; [self setTitle:@"文字復制粘貼"]; //點擊這個按鈕將剪貼板的內容粘貼到title上 UIBarButtonItem *addButton = [[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(readFromPasteboard:)]autorelease]; [[self navigationItem] setRightBarButtonItem:addButton]; } // Customize the number of sections in the table view. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return 9; } // Customize the appearance of table view cells. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier =@"Cell"; CopyTableViewCell *cell = (CopyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; if (cell == nil) { cell = [[[CopyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; [cell setDelegate:self]; } // Configure the cell. NSString *text = [NSString stringWithFormat:@"Row %d", [indexPath row]]; [[cell textLabel] setText:text]; return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if([self isMenuVisible]) { return; } [[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES animated:YES]; } //顯示菜單 - (void)showMenu:(id)cell { if ([cell isHighlighted]) { [cell becomeFirstResponder]; UIMenuController * menu = [UIMenuController sharedMenuController]; [menu setTargetRect: [cell frame] inView: [self view]]; [menu setMenuVisible: YES animated: YES]; } } - (void)readFromPasteboard:(id)sender { [self setTitle:[NSString stringWithFormat:@"Pasteboard = %@", [[UIPasteboard generalPasteboard] string]]]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Relinquish ownership any cached data, images, etc that aren't in use. } - (void)viewDidUnload { [super viewDidUnload]; [self.tableView release]; // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand. // For example: self.myOutlet = nil; }
效果:
復制一行數據:
點擊右上角的按鈕粘貼,將數據顯示在title上:
2、圖片復制粘貼
下面通過一個例子,將圖片復制和剪貼到另外一個UIImageView中間。
1、在界面上放置兩個uiimageview,一個是圖片的數據源,一個是將圖片粘貼到的地方。CopyPasteImageViewController 代碼如下:
- @interface CopyPasteImageViewController : UIViewController {
- UIImageView *imageView;
- UIImageView *pasteView;
- UIImageView *selectedView;
- }
- @property (nonatomic, retain) IBOutlet UIImageView *imageView;
- @property (nonatomic, retain) IBOutlet UIImageView *pasteView;
- @property (nonatomic, retain) UIImageView *selectedView;
- - (void)placeImageOnPasteboard:(id)view;
- @end
@interface CopyPasteImageViewController : UIViewController { UIImageView *imageView; UIImageView *pasteView; UIImageView *selectedView; } @property (nonatomic, retain) IBOutlet UIImageView *imageView; @property (nonatomic, retain) IBOutlet UIImageView *pasteView; @property (nonatomic, retain) UIImageView *selectedView; - (void)placeImageOnPasteboard:(id)view; @end
2、當觸摸圖片的時候我們顯示快捷菜單:
- - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
- NSSet *copyTouches = [event touchesForView:imageView];
- NSSet *pasteTouches = [event touchesForView:pasteView];
- [self becomeFirstResponder];
- if ([copyTouches count] > 0) {
- [self performSelector:@selector(showMenu:) withObject:imageView afterDelay:0.9f];
- } else if([pasteTouches count] > 0) {
- [self performSelector:@selector(showMenu:) withObject:pasteView afterDelay:0.9f];
- }
- [super touchesBegan:touches withEvent:event];
- }
- - (void)showMenu:(id)view {
- [self setSelectedView:view];
- UIMenuController * menu = [UIMenuController sharedMenuController];
- [menu setTargetRect: CGRectMake(5, 10, 1, 1) inView: view];
- [menu setMenuVisible: YES animated: YES];
- }
- 這里的快捷菜單,顯示三個菜單項:剪貼、粘貼、復制:
- - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
- if (action == @selector(cut:)) { return ([self selectedView] == imageView) ? YES : NO;
- } else if (action == @selector(copy:)) { return ([self selectedView] == imageView) ? YES : NO;
- } else if (action == @selector(paste:)) { return ([self selectedView] == pasteView) ? YES : NO;
- } else if (action == @selector(select:)) { return NO;
- } else if (action == @selector(selectAll:)) {
- return NO;
- } else {
- return [super canPerformAction:action withSender:sender];
- }
- }
- - (void)cut:(id)sender {
- [self copy:sender];
- [imageView setHidden:YES];
- }
- - (void)copy:(id)sender {
- [self placeImageOnPasteboard:[self imageView]]; }
- - (void)paste:(id)sender {
- UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyPasteImage" create:YES];
- NSData *data =[appPasteBoard dataForPasteboardType:@"com.marizack.CopyPasteImage.imageView"];
- pasteView.image = [UIImage imageWithData:data]; }
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event { NSSet *copyTouches = [event touchesForView:imageView]; NSSet *pasteTouches = [event touchesForView:pasteView]; [self becomeFirstResponder]; if ([copyTouches count] > 0) { [self performSelector:@selector(showMenu:) withObject:imageView afterDelay:0.9f]; } else if([pasteTouches count] > 0) { [self performSelector:@selector(showMenu:) withObject:pasteView afterDelay:0.9f]; } [super touchesBegan:touches withEvent:event]; } - (void)showMenu:(id)view { [self setSelectedView:view]; UIMenuController * menu = [UIMenuController sharedMenuController]; [menu setTargetRect: CGRectMake(5, 10, 1, 1) inView: view]; [menu setMenuVisible: YES animated: YES]; } 這里的快捷菜單,顯示三個菜單項:剪貼、粘貼、復制: - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{ if (action == @selector(cut:)) { return ([self selectedView] == imageView) ? YES : NO; } else if (action == @selector(copy:)) { return ([self selectedView] == imageView) ? YES : NO; } else if (action == @selector(paste:)) { return ([self selectedView] == pasteView) ? YES : NO; } else if (action == @selector(select:)) { return NO; } else if (action == @selector(selectAll:)) { return NO; } else { return [super canPerformAction:action withSender:sender]; } } - (void)cut:(id)sender { [self copy:sender]; [imageView setHidden:YES]; } - (void)copy:(id)sender { [self placeImageOnPasteboard:[self imageView]]; } - (void)paste:(id)sender { UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:@"CopyPasteImage" create:YES]; NSData *data =[appPasteBoard dataForPasteboardType:@"com.marizack.CopyPasteImage.imageView"]; pasteView.image = [UIImage imageWithData:data]; }
效果:
1、點擊圖片,顯示菜單按鈕。
2、點擊復制,將數據復制到剪貼板上:
3、點擊粘貼,將數據粘貼到uiimageview上。