概述:
在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
实现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
实现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; }
效果:
复制一行数据:
点击右上角的按钮粘贴,将数据显示在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
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]; }
效果:
1、点击图片,显示菜单按钮。
2、点击复制,将数据复制到剪贴板上:
3、点击粘贴,将数据粘贴到uiimageview上。
总结:本文详解了iOS系统应用程序中如何使用剪贴板。
作者:朱祁林
出处:http://zhuqil.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
发表评论
-
选取相册视频,并获得路径。
2012-07-18 10:18 1662- (NSString*)videoAssetURLT ... -
ad-hoc及企业版app打包之后在itunes中图标为白色的问题
2012-07-03 16:36 1248准备512*512大小的png(将要显示的图片),在终端执行以 ... -
iOS工具类之16进制颜色转为UIColor
2012-06-27 16:33 6712将16进制颜色如#FF0000,转换为UIColor: ... -
调试内存问题
2012-06-12 10:14 679Project -> Edit Scheme -> ... -
过滤html标签(修改版)
2011-12-01 10:44 1265- (NSString *)flattenHTML:(NS ... -
[转] iPhone:UIImage 图像截取,等比例缩放
2011-09-29 10:02 7770@interface UIImage(UIImageSc ... -
[转载]iPhone文件系统NSFileManager
2011-09-09 11:41 1016iPhone文件系统:创建、重命名以及删除文件 NSFile ... -
UIImage用imageNamed的内存问题
2011-09-08 09:33 1228@implementation UIImage(imageNa ... -
截屏存图片到相册或者指定路径
2011-06-13 11:15 2714#import <QuartzCore/Quartz ... -
存图片和视频到iPhone相册
2011-04-19 09:33 7594UIImage *img = [UIImage i ... -
选择相册图片并保存
2011-04-19 08:18 1758.h中: <UINavigationControl ... -
Push一个横屏页面
2011-03-28 16:39 2060[[UIDevice currentDevice] setOr ... -
NSPredicate及正则的一点使用
2011-03-21 14:38 6872记录一点正则表达式的使用。 例如:判断字符串首字母是否为字母 ... -
建立到服务器的连接
2011-02-14 10:47 1313在Mac OS X上,可以使用NSHost和NSS ... -
iPhone中的Sqlite3数据库操作
2010-12-27 09:38 1288可将数据库操作写入一个类,这样用起来也很方便。例如: ... -
Cocos2d游戏场景切换
2010-12-15 09:51 2615在游戏中,我们需要点击菜单选项进入相应的画面,而我们常 ... -
NSNotificationCenter随笔
2010-12-10 13:21 1470可在AppDelegate.h中定义,之后可全局使用。 ... -
Category的使用
2010-12-09 16:17 1011Category的用法有时使得程序很方便,可将已有的类 ... -
iPhone判断是否接入网络
2010-12-06 10:41 2349在这里介绍一种较为简单的判断是否连接网络的方法,首先要引入 ... -
手机问题汇总。
2010-09-16 17:24 0主题:http://bbs.dospy.com/blog.ph ...
相关推荐
iOS软件开发之详解剪贴板.pdf
在iOS中,可以使用剪贴板实现应用程序之中以及应用程序之间实现数据的共享。比如你可以从iPhone QQ复制一个url,然后粘贴到safari浏览器中查看这个链接的内容
深入讲解iOS开发入门以及应用,重点讲解了UI设计方面的相关基础及实例;包含多套原版资料以及详实的案例分享,可查看同名资料,绝对超值。
高仿微信,iOS应用开发模板.zip ios 开发模板。高仿微信,iOS应用开发模板.zip ios 开发模板。高仿微信,iOS应用开发模板.zip ios 开发模板。高仿微信,iOS应用开发模板.zip ios 开发模板。高仿微信,iOS应用开发...
iOS开发之美光盘源代码,由于视频太大,没有上传。
史上最全的IOS开发流程详解(绝密),详细描述了,IOS,注册账号,到上传包文件的整个过程。
在unity中,将文本复制到IOS剪贴板,可以使用原生的剪切板
《iOS7:iPhone/iPad应用开发技术详解》主要该书最后两章,主要两个示例的源代码是以XCode 5.2下进行调试通过,其中还包含了第19章,针对Xcode 5.2进行修改和调试的文档,密码,请根据该书提供的BLOG,获取密码
该文档是我在做完一个地图导航的项目之后做的一个总结!主要介绍如何使用iphone中的mapkit进行地图或者导航的开发!如果能下载到掌中全房这个iphone手机上的应用,可以参照的看!
ios开发之归档
全书分为两篇共18章,基础篇详细讲解了iOS技术概述、网页设计基础、HTML5基础、为iOS开发一个简单网页、移动化页面布局、设置导航、图片处理、设置文字、设置表单、实现搜索、分类和过滤效果、图表、HTML5在移动领域...
ios+android+h5复制剪切板
唐巧的《iOS开发进阶》,希望对一些初级的程序员有用
IOS UICollectionView布局详解
第一部分介绍iOS 开发的常用工具,第二部分介绍iOS开发中的一些常见的实践经验,第三部分介绍iOS 开发中涉及的原理。 如果把成为iOS 开发高手的过程比作武侠小说中的修炼过程的话,工具、实践和理论的学习就分别对应...
资源名称:《iOS开发零基础入门教程》(40集)资源目录:【】传智播客《iOS开发零基础入门教程》1.1【】传智播客《iOS开发零基础入门教程》1.2【】传智播客《iOS开发零基础入门教程》1.3【】传智播客《iOS开发零基础...
资源名称:iOS开发视频教程资源目录:【】iOS开发视频教程-第01讲-iOS历史介绍【】iOS开发视频教程-第02讲-XCode安装【】iOS开发视频教程-第03讲-UIView_PPT【】iOS开发视频教程-第04讲-UILabel【】iOS开发视频教程-...
ios开发流程ios开发流程