iOS开发应用剪贴板功能教程

移动开发 iOS
本文介绍的是IOS应用中的剪贴板的功能实现,很详细的讲解了所使用的类和控件等内容,代码实现,来看详细内容。

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

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

例子:

1、复制剪贴文本。

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

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

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

实现CopyTableViewCell :

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

定义CopyPasteTextController,实现粘贴功能。

  1. @interface CopyPasteTextController : UIViewController<UITableViewDelegate>   
  2. {      
  3. //用来标识是否显示快捷菜单      
  4. BOOL menuVisible;      
  5. UITableView *tableView;  
  6. }  
  7. @property (nonatomic, getter=isMenuVisible) BOOL menuVisible;  
  8. @property (nonatomic, retain) IBOutlet UITableView *tableView;  
  9. @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:@"文字复制粘贴"]; //点击这个按钮将剪贴板的内容粘贴到title上      
  8.  UIBarButtonItem *addButton = [[[UIBarButtonItem alloc]               
  9.   initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh      
  10.     target:self  
  11.      action:@selector(readFromPasteboard:)]  
  12.      autorelease];      
  13.  [[self navigationItem] setRightBarButtonItem:addButton];  
  14.  }  
  15.  // Customize the number of sections in the table view.  
  16.  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{   
  17.     return 1;  
  18.  }  
  19.  - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{    
  20.    return 9;}// Customize the appearance of table view cells.  
  21.    - (UITableViewCell *)tableView:(UITableView *)tableView   
  22.    cellForRowAtIndexPath:(NSIndexPath *)indexPath{    
  23.   static NSString *CellIdentifier =@"Cell";    
  24.     CopyTableViewCell *cell = (CopyTableViewCell *)  
  25.     [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. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{    
  36.   if([self isMenuVisible])    {        
  37.     return;   
  38.  }     
  39.   [[[self tableView] cellForRowAtIndexPath:indexPath] setSelected:YES                           
  40.                                  animated:YES];}//显示菜单- (void)showMenu:(id)cell {    
  41.     if ([cell isHighlighted]) {      
  42.         [cell becomeFirstResponder];  
  43.        UIMenuController * menu = [UIMenuController sharedMenuController];  
  44.       [menu setTargetRect: [cell frame] inView: [self view]];   
  45.    [menu setMenuVisible: YES animated: YES];  
  46.  }  
  47.  }  
  48. - (void)readFromPasteboard:(id)sender {    
  49.   [self setTitle:[NSString stringWithFormat:@"Pasteboard = %@",[[UIPasteboard generalPasteboard] string]]];  
  50. }  
  51. - (void)didReceiveMemoryWarning{     
  52.  // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning];   
  53.         // Relinquish ownership any cached data, images, etc that aren't in use.  
  54.     }  
  55.  - (void)viewDidUnload{      
  56.  [super viewDidUnload];     
  57.   [self.tableView release];   
  58.   // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.     
  59.    // For example: self.myOutlet = nil;  

效果:

iOS开发应用剪贴板功能教程

复制一行数据:

iOS开发应用剪贴板功能教程

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

#p#

2、图片复制粘贴

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

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

  1. @interface CopyPasteImageViewController : UIViewController {     
  2.  UIImageView *imageView;      
  3.  UIImageView *pasteView;     
  4.   UIImageView *selectedView;  
  5. }@property (nonatomic, retain) IBOutlet UIImageView *imageView;  
  6. @property (nonatomic, retain) IBOutlet UIImageView *pasteView;  
  7. @property (nonatomic, retain) UIImageView *selectedView;  
  8. - (void)placeImageOnPasteboard:(id)view;  
  9. @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:)   
  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];} 

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

  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 =[UIPasteboard pasteboardWithName:@"CopyPasteImage" create:YES];      
  25.  NSData *data =[appPasteBoard dataForPasteboardType:@"com.marizack.CopyPasteImage.imageView"];     
  26.   pasteView.image = [UIImage imageWithData:data];  

效果:

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

iOS开发应用剪贴板功能教程

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

iOS开发应用剪贴板功能教程

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

iOS开发应用剪贴板功能教程

小结:iOS开发应用剪贴板功能教程的内容介绍完了,希望通过本文的学习对你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2009-12-18 14:10:29

Ruby访问剪贴板

2010-02-02 17:47:59

C++操作剪贴板

2024-04-09 08:27:01

Android高效管理数据

2022-03-31 22:53:47

Windows 11太阳谷2智能剪贴板

2021-12-12 09:42:48

Windows 11桌面微软

2021-12-05 09:28:18

Windows 11操作系统微软

2021-07-29 09:55:59

鸿蒙HarmonyOS应用

2020-12-14 05:57:01

clipboard.Selection execCommand

2009-08-10 17:37:54

2016-05-11 15:01:31

Linux剪贴板管理器

2020-07-02 07:53:59

App操作系统应用

2021-12-02 10:11:44

鸿蒙HarmonyOS应用

2020-10-12 09:40:57

Windows 10Windows操作系统

2018-03-23 10:15:28

Windows 10云剪贴板复制粘贴

2021-08-29 07:43:43

CopyQ操作系统微软

2021-08-03 07:06:54

Windows 11操作系统微软

2009-10-21 10:15:29

VB.NET复制

2021-11-30 05:37:51

App监听手机监管

2021-03-09 05:48:01

Windows10操作系统21H2

2023-02-06 07:17:22

点赞
收藏

51CTO技术栈公众号