天天看点

iOS开发值详解剪贴板  

在iOS中,可以使用剪贴板实现应用程序之中以及应用程序之间实现数据的共享。比如你可以从iPhone QQ复制一个url,然后粘贴到safari浏览器中查看这个链接的内容。 概述 在iOS中下面三个

在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:来创建。

了解这些之后,下面通过一系列的例子来说明如何在应用程序中使用剪贴板。

例子:

一、复制剪贴文本。

    下面通过一个例子,可以在tableview上显示一个快捷菜单,上面只有复制按钮,复制tableview上的数据之后,然后粘贴到title上。

定义一个单元格类CopyTableViewCell,在这个类的上显示快捷菜单,实现复制功能。

 @interface CopyTableViewCell : UITableViewCell {

    id delegate;

}

@property (nonatomic, retain) id delegate;

@end

实现CopyTableViewCell ,实现粘贴:

view plain

  1. #import "CopyTableViewCell.h"  
  2. @implementation CopyTableViewCell  
  3. @synthesize delegate;  
  4. - (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {  
  5.     if ((self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])) {  
  6.     }  
  7.     return self;  
  8. }  
  9. - (void)setSelected:(BOOL)selected animated:(BOOL)animated {  
  10.     [super setSelected:selected animated:animated];  
  11. }  
  12. - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {  
  13.     [[self delegate] performSelector:@selector(showMenu:)   
  14.                           withObject:self afterDelay:0.9f];  
  15.     [super setHighlighted:highlighted animated:animated];  
  16. }  
  17. - (BOOL)canBecomeFirstResponder   
  18. {  
  19.     return YES;  
  20. }  
  21. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{  
  22.     if (action == @selector(cut:)){  
  23.         return NO;  
  24.     }   
  25.     else if(action == @selector(copy:)){  
  26.         return YES;  
  27.     }   
  28.     else if(action == @selector(paste:)){  
  29.         return NO;  
  30.     }   
  31.     else if(action == @selector(select:)){  
  32.         return NO;  
  33.     }   
  34.     else if(action == @selector(selectAll:)){  
  35.         return NO;  
  36.     }  
  37.     else   
  38.     {  
  39.         return [super canPerformAction:action withSender:sender];  
  40.     }  
  41. }  
  42. - (void)copy:(id)sender {  
  43.     UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];  
  44.     [pasteboard setString:[[self textLabel]text]];  
  45. }  
  46. - (void)dealloc {  
  47.     [super dealloc];  
  48. }  
  49. @end  

定义CopyPasteTextController

view plain

  1. @interface CopyPasteTextController : UIViewController<UITableViewDelegate> {  
  2.     //用来标识是否显示快捷菜单  
  3.     BOOL menuVisible;  
  4.     UITableView *tableView;  
  5. }  
  6. @property (nonatomic, getter=isMenuVisible) BOOL menuVisible;  
  7. @property (nonatomic, retain) IBOutlet UITableView *tableView;  
  8. @end   

实现CopyPasteTextController :

view plain

  1. #import "CopyPasteTextController.h"  
  2. #import "CopyTableViewCell.h"  
  3. @implementation CopyPasteTextController  
  4. @synthesize menuVisible,tableView;  
  5. - (void)viewDidLoad {  
  6.     [super viewDidLoad];  
  7.     [self setTitle:@"文字复制粘贴"];  
  8.     //点击这个按钮将剪贴板的内容粘贴到title上  
  9.     UIBarButtonItem *addButton = [[[UIBarButtonItem alloc]   
  10.                                       initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh  
  11.                                       target:self  
  12.                                       action:@selector(readFromPasteboard:)]   
  13.                                      autorelease];  
  14.     [[self navigationItem] setRightBarButtonItem:addButton];  
  15. }  
  16. // Customize the number of sections in the table view.  
  17. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView  
  18. {  
  19.     return 1;  
  20. }  
  21. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section  
  22. {  
  23.     return 9;  
  24. }  
  25. // Customize the appearance of table view cells.  
  26. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  
  27. {  
  28.     static NSString *CellIdentifier =@"Cell";  
  29.     CopyTableViewCell *cell = (CopyTableViewCell *)[tableView   
  30.                                                            dequeueReusableCellWithIdentifier:CellIdentifier];  
  31.     if (cell == nil)   
  32.     {  
  33.         cell = [[[CopyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];  
  34.         [cell setDelegate:self];  
  35.     }  
  36.     // Configure the cell.  
  37.     NSString *text = [NSString stringWithFormat:@"Row %d", [indexPath row]];  
  38.     [[cell textLabel] setText:text];  
  39.     return cell;  
  40. }  
  41. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  
  42. {  
  43.     if([self isMenuVisible])  
  44.     {  
  45.         return;  
  46.     }  
  47.     [[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES   
  48.                                                            animated:YES];  
  49. }  
  50. //显示菜单  
  51. - (void)showMenu:(id)cell {  
  52.     if ([cell isHighlighted]) {  
  53.         [cell becomeFirstResponder];  
  54.         UIMenuController * menu = [UIMenuController sharedMenuController];  
  55.         [menu setTargetRect: [cell frame] inView: [self view]];  
  56.         [menu setMenuVisible: YES animated: YES];  
  57.     }  
  58. }  
  59. - (void)readFromPasteboard:(id)sender {  
  60.     [self setTitle:[NSString stringWithFormat:@"Pasteboard = %@",   
  61.                       [[UIPasteboard generalPasteboard] string]]];  
  62. }  
  63. - (void)didReceiveMemoryWarning  
  64. {  
  65.     // Releases the view if it doesn't have a superview.  
  66.     [super didReceiveMemoryWarning];  
  67.     // Relinquish ownership any cached data, images, etc that aren't in use.  
  68. }  
  69. - (void)viewDidUnload  
  70. {  
  71.     [super viewDidUnload];  
  72.     [self.tableView release];  
  73.     // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.  
  74.     // For example: self.myOutlet = nil;  
  75. }  

效果:

复制一行数据:

iOS开发值详解剪贴板  

点击右上角的按钮粘贴,将数据显示在title上:

iOS开发值详解剪贴板  

二、图片复制粘贴

   下面通过一个例子,将图片复制和剪贴到另外一个UIImageView中间。

1、在界面上放置两个uiimageview,一个是图片的数据源,一个是将图片粘贴到的地方。CopyPasteImageViewController 代码如下:

view plain

  1. @interface CopyPasteImageViewController : UIViewController {  
  2.     UIImageView *imageView;  
  3.     UIImageView *pasteView;  
  4.     UIImageView *selectedView;  
  5. }  
  6. @property (nonatomic, retain) IBOutlet UIImageView *imageView;  
  7. @property (nonatomic, retain) IBOutlet UIImageView *pasteView;  
  8. @property (nonatomic, retain) UIImageView *selectedView;  
  9. - (void)placeImageOnPasteboard:(id)view;  
  10. @end  

2、当触摸图片的时候我们显示快捷菜单:

view plain

  1. - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {  
  2.     NSSet *copyTouches = [event touchesForView:imageView];  
  3.     NSSet *pasteTouches = [event touchesForView:pasteView];  
  4.     [self becomeFirstResponder];  
  5.     if ([copyTouches count] > 0) {  
  6.         [self performSelector:@selector(showMenu:)   
  7.                    withObject:imageView afterDelay:0.9f];  
  8.     }  
  9.     else  if([pasteTouches count] > 0) {  
  10.         [self performSelector:@selector(showMenu:)   
  11.                    withObject:pasteView afterDelay:0.9f];  
  12.     }  
  13.     [super touchesBegan:touches withEvent:event];  
  14. }  
  15. - (void)showMenu:(id)view {  
  16.     [self setSelectedView:view];  
  17.     UIMenuController * menu = [UIMenuController sharedMenuController];  
  18.     [menu setTargetRect: CGRectMake(5, 10, 1, 1) inView: view];  
  19.     [menu setMenuVisible: YES animated: YES];  
  20. }  

这里的快捷菜单,显示三个菜单项:剪贴、粘贴、复制:

view plain

  1. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{  
  2.     if (action == @selector(cut:)) {  
  3.         return ([self selectedView] == imageView) ? YES : NO;  
  4.     } else if (action == @selector(copy:)) {  
  5.         return ([self selectedView] == imageView) ? YES : NO;  
  6.     } else if (action == @selector(paste:)) {  
  7.         return ([self selectedView] == pasteView) ? YES : NO;  
  8.     } else if (action == @selector(select:)) {  
  9.         return NO;  
  10.     } else if (action == @selector(selectAll:)) {  
  11.         return NO;  
  12.     } else {  
  13.         return [super canPerformAction:action withSender:sender];  
  14.     }  
  15. }  
  16. - (void)cut:(id)sender {  
  17.     [self copy:sender];  
  18.     [imageView setHidden:YES];  
  19. }  
  20. - (void)copy:(id)sender {  
  21.     [self placeImageOnPasteboard:[self imageView]];  
  22. }  
  23. - (void)paste:(id)sender {  
  24.     UIPasteboard *appPasteBoard =   
  25.     [UIPasteboard pasteboardWithName:@"CopyPasteImage" create:YES];  
  26.     NSData *data =[appPasteBoard dataForPasteboardType:@"com.marizack.CopyPasteImage.imageView"];  
  27.     pasteView.image = [UIImage imageWithData:data];  
  28. }  

效果:

1、点击图片,显示菜单按钮。

iOS开发值详解剪贴板  

2、点击复制,将数据复制到剪贴板上:

iOS开发值详解剪贴板  

3、点击粘贴,将数据粘贴到uiimageview上。

iOS开发值详解剪贴板  

原文链接:http://blog.csdn.net/zhuqilin0/article/details/6661044