ios开发读取剪切板的内容_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,在这个类的上显示快捷菜单,实现复制功能。
@interface CopyTableViewCell : UITableViewCell {
id delegate;
}
@property (nonatomic, retain) id delegate;
@end
@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])) { }
returnself;
}
- (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 {
returnYES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
if(action == @selector(cut:)){
returnNO;
}elseif(action == @selector(copy:)){
returnYES;
}elseif(action == @selector(paste:)){
returnNO;
}elseif(action == @selector(select:)){
returnNO;
}elseif(action == @selector(selectAll:)){
returnNO;
}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 {
//用来标识是否显示快捷菜单
BOOLmenuVisible;
UITableView *tableView;
}
@property (nonatomic, getter=isMenuVisible)BOOLmenuVisible;
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@end
#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 {
//用来标识是否显示快捷菜单
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 {
return1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return9;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
staticNSString *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];
returncell;
}
- (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;
}
#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
@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];
}elseif([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;
}elseif(action == @selector(copy:)) {return([self selectedView] == imageView) ? YES : NO;
}elseif(action == @selector(paste:)) {return([self selectedView] == pasteView) ? YES : NO;
}elseif(action == @selector(select:)) {returnNO;
}elseif(action == @selector(selectAll:)) {
returnNO;
}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]; }
- (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开发读取剪切板的内容_iOS开发_iphone 实现剪贴板操作_iphone 复制粘贴功能(转)...相关推荐
- ios开发读取剪切板的内容_ios开发读取剪切板的内容_苹果隐私问题堪忧!多个iOS应用未经许可读取剪贴板......
[CNMO新闻]近日,据外媒Macrumors报道,根据最新研究显示,数十种流行的iOS应用程序,在未经用户同意的情况下读取剪贴板的内容,其中可能包含敏感信息.开发人员发现,似乎有相当多的iOS应用程 ...
- ios开发读取剪切板的内容_iOS中管理剪切板的UIPasteboard粘贴板类用法详解
一.自带剪切板操作的原生UI控件在iOS的UI系统中,有3个控件自带剪切板操作,分别是UITextField.UITextView与UIWebView.在这些控件的文字交互处进行长按手势可以在屏幕视图 ...
- ios开发读取剪切板的内容_为你找到3款Mac平台好用的剪切板工具,你值得拥有!...
不知道大家有没有这样的体会,我们在进行文字编辑的时候,复制下来的文字常常需要重复使用,但新的内容一旦复制,旧的内容就被覆盖清理了.因此选择一款易用高效的剪贴板成了很多人都有的需求. 有些朋友可能会说, ...
- Python读取剪切板的内容并转为list
记录一下,在剪切板里有一段数据: 1 29.003119754802746 26.262735448439088 -236.55271565495212 0 0 0 1 1 0 0 F-1 vtkMR ...
- 计算机关闭后剪切板的内容会消失,清除win10剪贴板历史记录,保证隐私数据不泄露...
在Windows中复制某些内容并将其粘贴到其他位置时,剪贴板上的数据不会消失.如果你想清除Windows剪贴板的历史记录并将剪贴板保留下来,可以使用下面几种不同的方法可以执行此操作. 什么是剪贴板历史 ...
- iOS开发_iphone 实现剪贴板操作_iphone 复制粘贴功能
在iOS中,可以使用剪贴板实现应用程序之中以及应用程序之间实现数据的共享.比如你可以从iPhone QQ复制一个url,然后粘贴到safari浏览器中查看这个链接的内容. 一.在iOS中下面三个控件, ...
- java剪切txt文件_用Java把剪切板的内容实时保存到txt
test类:提示用户程序已启动,提示保存位置,清空剪切板. package com.ariya.service; import com.ariya.service.impl.ClipboardServ ...
- JS复制图片到剪切板 读取剪切板
JS复制图片到剪切板 读取剪切板 navigator.clipboard实现复制图片 图片写入剪切板 function handleCopyImg() { const canvas = documen ...
- html5 读取剪切板,Html5剪切板功能的实现
本篇文章主要介绍了Html5剪切板功能的实现代码,内容挺不错的,现在分享给大家,也给大家做个参考. 最近使用Vue开发Line(日韩的一款类似中国微信平台)的内嵌H5.里面的有一个需求就是将当前链接粘 ...
最新文章
- linux-shell数据重定向详细分析
- android 默认开关,android默认设置的开关
- 如何用Java创建不可变的Map
- JS-封装js让一个div或者img的移动
- python提示对话框自动关闭_Python实现定时自动关闭的tkinter窗口方法
- nodejs异步流程控制
- AT3 two-dimensional surfaces : the sphere
- python简单的聚类分析代码_python kmeans聚类简单介绍和实现代码
- NLP+词法系列(二)︱中文分词技术简述、深度学习分词实践(CIPS2016、超多案例)
- 协作中继认知无线电功率分配
- STM32开发 | 移远4G-Cat.1模组EC200N-CN开发
- 绿色版Mysql数据库快速搭建
- 通过上位机软件测试总线舵机
- 开机后黑屏看不到桌面_开机不显示桌面黑屏怎么办_win10开机黑屏啥也没有的解决办法...
- 基于RT1052 Aworks 测试PXP图像混合功能(十三)
- 使用lio_sam建图,然后使用LIO-SAM_based_relocalization-master导航
- 【STM32训练—TOF激光测距模块】第一篇、STM32驱动TOF10120测量距离
- HDU 3473 Minimum Sum 【划分树】
- java成绩五分制转换_绩点五分制换算(5分制的绩点对照表)
- cad模型轻量化_UNISOL告诉你,CAD与VR能迸发出怎样的火花