iOS開發_iphone 實現剪貼板操作_iphone 複製粘貼功能

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,在這個類的上顯示快捷菜單,實現複製功能。

  1. @interface CopyTableViewCell : UITableViewCell {       
  2.     id delegate;   
  3. }   
  4. @property (nonatomic, retain) id delegate;   
  5. @end  

//實現CopyTableViewCell :

  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.     return self;   
  7. }   
  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:) withObject:self afterDelay:0.9f];            
  14.     [super setHighlighted:highlighted animated:animated];    
  15. }   
  16. - (BOOL)canBecomeFirstResponder  {      
  17.     return YES;   
  18. }   
  19.   
  20. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{       
  21.     if (action == @selector(cut:)){           
  22.         return NO;       
  23.     }else if(action == @selector(copy:)){           
  24.             return YES;       
  25.     } else if(action == @selector(paste:)){           
  26.             return NO;       
  27.     } else if(action == @selector(select:)){           
  28.             return NO;       
  29.     }      else if(action == @selector(selectAll:)){           
  30.             return NO;       
  31.     }     else      {           
  32.     return [super canPerformAction:action withSender:sender];      
  33.     }   
  34. }   
  35. - (void)copy:(id)sender {       
  36.     UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];       
  37.     [pasteboard setString:[[self textLabel]text]]; }   
  38. - (void)dealloc {       
  39.     [super dealloc];   
  40. }   
  41. @end  
  42.    
  43. //定義CopyPasteTextController,實現粘貼功能。  
  44. @interface CopyPasteTextController : UIViewController<UITableViewDelegate> {       
  45.     //用來標識是否顯示快捷菜單       
  46.     BOOL menuVisible;       
  47.     UITableView *tableView;   
  48. }    
  49. @property (nonatomic, getter=isMenuVisible) BOOL menuVisible;    
  50. @property (nonatomic, retain) IBOutlet UITableView *tableView;   
  51. @end   

實現CopyPasteTextController :

  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]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh                                                   target:self                                         
  10.                             action:@selector(readFromPasteboard:)]autorelease];       
  11.     [[self navigationItem] setRightBarButtonItem:addButton];   
  12. }     
  13.   
  14. // Customize the number of sections in the table view.   
  15. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {       
  16.     return 1;   
  17. }    
  18.   
  19. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {       
  20.     return 9;   
  21. }    
  22. // Customize the appearance of table view cells.   
  23. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {       
  24.     static NSString *CellIdentifier =@"Cell";       
  25.     CopyTableViewCell *cell = (CopyTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];       
  26.     if (cell == nil) {           
  27.         cell = [[[CopyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];           
  28.     [cell setDelegate:self];       
  29.     }            
  30.     // Configure the cell.       
  31.     NSString *text = [NSString stringWithFormat:@"Row %d", [indexPath row]];       
  32.     [[cell textLabel] setText:text];       
  33.     return cell;   
  34. }    
  35.   
  36. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {       
  37.     if([self isMenuVisible])     {           
  38.     return;       
  39. }       
  40.     [[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES  animated:YES];   
  41. }   
  42.   
  43. //顯示菜單   
  44. - (void)showMenu:(id)cell {       
  45.     if ([cell isHighlighted]) {           
  46.     [cell becomeFirstResponder];                    
  47.     UIMenuController * menu = [UIMenuController sharedMenuController];           
  48.     [menu setTargetRect: [cell frame] inView: [self view]];           
  49.     [menu setMenuVisible: YES animated: YES];       
  50. }   
  51. }   
  52.   
  53. - (void)readFromPasteboard:(id)sender {       
  54.     [self setTitle:[NSString stringWithFormat:@"Pasteboard = %@",                          
  55.     [[UIPasteboard generalPasteboard] string]]];   
  56. }    
  57. - (void)didReceiveMemoryWarning {       
  58. // Releases the view if it doesn't have a superview.       
  59.     [super didReceiveMemoryWarning];            
  60. // Relinquish ownership any cached data, images, etc that aren't in use.   
  61. }    
  62.   
  63. - (void)viewDidUnload {       
  64.     [super viewDidUnload];       
  65.     [self.tableView release];            
  66. // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.       
  67. // For example: self.myOutlet = nil;   
  68. }  

效果:

複製一行數據:

點擊右上角的按鈕粘貼,將數據顯示在title上:

2、圖片複製粘貼

   下面通過一個例子,將圖片複製和剪貼到另外一個UIImageView中間。

1、在界面上放置兩個uiimageview,一個是圖片的數據源,一個是將圖片粘貼到的地方。CopyPasteImageViewController 代碼如下:

  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、當觸摸圖片的時候我們顯示快捷菜單:

  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:) withObject:imageView afterDelay:0.9f];       
  7.     }     else  if([pasteTouches count] > 0) {           
  8.         [self performSelector:@selector(showMenu:) withObject:pasteView afterDelay:0.9f];       
  9.     }       
  10.     [super touchesBegan:touches withEvent:event];   
  11. }    
  12.   
  13. - (void)showMenu:(id)view {       
  14.     [self setSelectedView:view];            
  15.     UIMenuController * menu = [UIMenuController sharedMenuController];       
  16.     [menu setTargetRect: CGRectMake(5, 10, 1, 1) inView: view];       
  17.     [menu setMenuVisible: YES animated: YES];   
  18. }  
  19.   
  20. 這裏的快捷菜單,顯示三個菜單項:剪貼、粘貼、複製:  
  21. - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{       
  22.     if (action == @selector(cut:)) {         return ([self selectedView] == imageView) ? YES : NO;       
  23.     } else if (action == @selector(copy:)) {        return ([self selectedView] == imageView) ? YES : NO;       
  24.     } else if (action == @selector(paste:)) {         return ([self selectedView] == pasteView) ? YES : NO;       
  25.     } else if (action == @selector(select:)) {         return NO;       
  26.     } else if (action == @selector(selectAll:)) {           
  27.         return NO;      
  28.     } else {           
  29.         return [super canPerformAction:action withSender:sender];       
  30.     }   
  31. }   
  32.   
  33. - (void)cut:(id)sender {       
  34.     [self copy:sender];       
  35.     [imageView setHidden:YES];   
  36. }   
  37.   
  38. - (void)copy:(id)sender {       
  39.     [self placeImageOnPasteboard:[self imageView]]; }   
  40.   
  41. - (void)paste:(id)sender {       
  42.     UIPasteboard *appPasteBoard =      [UIPasteboard pasteboardWithName:@"CopyPasteImage" create:YES];       
  43.     NSData *data =[appPasteBoard dataForPasteboardType:@"com.marizack.CopyPasteImage.imageView"];       
  44.     pasteView.image = [UIImage imageWithData:data]; } 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章