iOS开发之详解剪贴板

关于UIMenuController的用法例子

今天终于搞明白了UIMenuController显示的相关内容,把源代码分享给大家!

要正常显示菜单,必须做到以下几点:
1. -(BOOL)canBecomeFirstResponder 必须返回YES

2. -(BOOL)canPerformAction:(SEL)action withSender:(id)sender
该函数中,要显示的菜单项(包括系统的菜单项)的方法必须返回YES

3. 在显示菜单前,必须调用:
[self becomeFirstResponder]
成为第一个响应者

4. 为了马上可以正常显示第二个菜单,必须使用:

[menuController setMenuVisible:NO];
先关闭一下,不然就显示不出来!这是我好不容易才发现的!!!大家鼓励下哦

在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 ,实现粘贴:

#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)ed animated:(BOOL)animated {  [super setSelected:ed animated:animated];  }  - (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated {  [[self delegate] performSelector:@or(showMenu:)  withObject:self afterDelay:0.9f];  

[super setHighlighted:highlighted animated:animated];  

}  - (BOOL)canBecomeFirstResponder  {  return YES;  }  - (BOOL)canPerformAction:(SEL)action withSender:(id)sender{  if (action == @or(cut:)){  return NO;  }  else if(action == @or(copy:)){  return YES;  }  else if(action == @or(paste:)){  return NO;  }  else if(action == @or(:)){  return NO;  }  else if(action == @or(All:)){  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:@or(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上:

二、图片复制粘贴

下面经由过程一个例子,将图片复制和剪贴到别的一个UIImageView中心。

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

@interface CopyPasteImageViewController : UIViewController {  UIImageView *imageView;  UIImageView *pasteView;  UIImageView *edView;  }  @property (nonatomic, retain) IBOutlet UIImageView *imageView;  @property (nonatomic, retain) IBOutlet UIImageView *pasteView;  @property (nonatomic, retain) UIImageView *edView;  - (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:@or(showMenu:)  withObject:imageView afterDelay:0.9f];  }  else  if([pasteTouches count] > 0) {  [self performSelector:@or(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 == @or(cut:)) {  return ([self edView] == imageView) ? YES : NO;  } else if (action == @or(copy:)) {  return ([self edView] == imageView) ? YES : NO;  } else if (action == @or(paste:)) {  return ([self edView] == pasteView) ? YES : NO;  } else if (action == @or(:)) {  return NO;  } else if (action == @or(All:)) {  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上。

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

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITextField *inputTF;

- (IBAction)cutAction:(UIButton *)sender;

- (IBAction)copyAction:(UIButton *)sender;

- (IBAction)pasteAction:(UIButton *)sender;

- (IBAction)mianban:(UIButton *)sender;

@property (weak, nonatomic) IBOutlet UILabel *textLabel;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.la

UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 50, 100)];

[self.view addSubview:label];

label.text = @"123456789";

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (IBAction)cutAction:(UIButton *)sender {

UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];

pasteBoard.string = self.inputTF.text;

self.inputTF.text = @"";

}

- (IBAction)copyAction:(UIButton *)sender {

UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];

pasteBoard.string = self.inputTF.text;

}

- (IBAction)pasteAction:(UIButton *)sender {

UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];

self.textLabel.text = pasteBoard.string;

}

- (IBAction)mianban:(UIButton *)sender {

UIMenuController * menu = [UIMenuController sharedMenuController];

[menu setTargetRect:CGRectMake(10, 100, 50, 100) inView: [self view]];

[menu setMenuVisible:NO animated: YES];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

[self.view endEditing:YES];

}

@end

时间: 2024-08-18 09:01:44

iOS开发之详解剪贴板的相关文章

iOS开发:详解Objective-C runTime

Objective-C总Runtime的那点事儿(一)消息机制 最近在找工作,Objective-C中的Runtime是经常被问到的一个问题,几乎是面试大公司必问的一个问题.当然还有一些其他问题也几乎必问,例如:RunLoop,Block,内存管理等.其他的问题如果有机会我会在其他文章中介绍. 本篇文章主要介绍RunTime. RunTime简称运行时.就是系统在运行的时候的一些机制,其中最主要的是消息机制.对于C语言,函数的调用在编译的时候会决定调用哪个函数( C语言的函数调用请看这里 ).编

iOS开发 - UIActivityViewController详解

昨天在做微信分享的时候, 用到了这个东西.趁热写点东西记录下. UIActivityViewController类是一个标准的view controller,通个使用这个controller,你的应用程序就可以提供各种服务. 系统提供了一些通用的标准服务,例如拷贝内容至粘贴板.发布一个公告至社交网.通过email或者SMS发送内容. 应用程序同样可以自定义服务.(我的微信分享就属于自定义服务, 之后将会写一篇教程介绍) 你的应用程序负责配置.展现和解雇这个view controller. vie

IOS开发 Blocks详解(转)

IOS开发 Blocks详解(转) (2013-10-14 16:41:54) 从Mac OS X 10.6以及iOS 4开始,苹果在GCC和Clang编译器中为C语言引入了一个新扩展:Blocks,使得程序员可以在C.Objective-C.C++和Objective-C中使用闭包.Blocks有点像函数,但是它可以在其它函数或方法中进行声明和定义,同时它还是匿名的(匿名函数),并可以捕获其所在作用域中的变量(闭包特性). Blocks的语法 Blocks和C语言中的函数指针有点类似,如果你了

iOS开发- UICollectionView详解+实例

iOS开发- UICollectionView详解+实例 本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView. UICollectionView 和 UICollectionViewController 类是iOS6 新引进的API,用于展示集合视图,布局更加灵活,可实现多列布局,用法类似于UITableView 和 UITableViewController 类. 使用UICollectionView 必须实现UICol

iOS开发——MVC详解&amp;Swift+OC

MVC 设计模式 这两天认真研究了一下MVC设计模式,在iOS开发中这个算是重点中的重点了,如果对MVC模式不理解或者说不会用,那么你iOS肯定学不好,或者写不出好的东西,当然本人目前也在学习中,不过既然能看到这篇文档,说明你已经开始着手学习并且想深入研究它了,个人也是研究很久才搞懂,就写下来希望对各位有用,也能方便自己以后开发中查看,好了废话不多说,下面就来详细介绍一下MVC,并且用实例验证一下在项目开发中怎么去使用它. 相信你对 MVC 设计模式 并不陌生,只是不能完全理解其中的含义或者不能

IOS开发之----详解在IOS后台执行

文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后台运行一小段时间. 还有三种类型的可以运行在后以,1.音乐2.location 3.voip 文二 在IOS后台执行是本文要介绍的内容,大多数应用程序进入后台状态不久后转入暂停状态.在这种状态下,应用程序不执行任何代码,并有可能在任意时候从内存中删除.应用程序提供特定的服务,用户可以请求后台执行时间,以提供这些服务. 判断是否支持多线程 UIDevice* device = [UIDevice c

iOS开发--Bison详解连连支付集成简书

"最近由于公司项目需要集成连连支付,文档写的不是很清楚,遇到了一些坑,因此记录一下,希望能帮到有需要的人." 前面简单的集成没有遇到什么坑,在此整理一下官方的集成文档,具体步骤如下 导入文件 添加头文件引用 设置link标志Target->Build Setting ,Other Linker Flags 设置为 -all_load可能添加-all_load以后和其他库冲突,可以尝试使用 -force_load 单独load库, force_load后面跟的是 lib库的完整路径

iOS开发-NSURLSession详解

Core Foundation中NSURLConnection在2003年伴随着Safari浏览器的发行,诞生的时间比较久远,iOS升级比较快,AFNetWorking在3.0版本删除了所有基于NSURLConnection API的所有支持,新的API完全基于NSURLSession.AFNetworking 1.0建立在NSURLConnection的基础之上 ,AFNetworking 2.0使用NSURLConnection基础API,以及较新基于NSURLSession的API的选项.

iOS开发-Runtime详解(简书)

简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [receiver message]; // 底层运行时会被编译器转化为: objc_msgSend(receiver, selector) // 如果其还有参数比如: [receiver message:(id)arg...]; // 底层运行时会被编译器转化为: objc_msgSend(receiver, selector, arg1,