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
iOS开发之详解剪贴板的更多相关文章
- iOS开发——Block详解
iOS开发--Block详解 1. Block是什么 代码块 匿名函数 闭包--能够读取其他函数内部变量的函数 函数变量 实现基于指针和函数指针 实现回调的机制 Block是一个非常有特色的语法,它可 ...
- iOS开发:详解Objective-C runTime
Objective-C总Runtime的那点事儿(一)消息机制 最近在找工作,Objective-C中的Runtime是经常被问到的一个问题,几乎是面试大公司必问的一个问题.当然还有一些其他问题也几乎 ...
- iOS开发-Runtime详解
iOS开发-Runtime详解 简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [recei ...
- iOS开发——MVC详解&Swift+OC
MVC 设计模式 这两天认真研究了一下MVC设计模式,在iOS开发中这个算是重点中的重点了,如果对MVC模式不理解或者说不会用,那么你iOS肯定学不好,或者写不出好的东西,当然本人目前也在学习中,不过 ...
- IOS开发之----详解在IOS后台执行
文一 我从苹果文档中得知,一般的应用在进入后台的时候可以获取一定时间来运行相关任务,也就是说可以在后台运行一小段时间. 还有三种类型的可以运行在后以,1.音乐2.location 3.voip 文二 ...
- iOS开发--Bison详解连连支付集成简书
"最近由于公司项目需要集成连连支付,文档写的不是很清楚,遇到了一些坑,因此记录一下,希望能帮到有需要的人." 前面简单的集成没有遇到什么坑,在此整理一下官方的集成文档,具体步骤如下 ...
- iOS开发-NSURLSession详解
Core Foundation中NSURLConnection在2003年伴随着Safari浏览器的发行,诞生的时间比较久远,iOS升级比较快,AFNetWorking在3.0版本删除了所有基于NSU ...
- iOS开发之详解正则表达式
本文由Charles翻自raywenderlich原文:NSRegularExpression Tutorial: Getting Started更新提示:本教程被James Frost更新到了iOS ...
- iOS开发-Runtime详解(简书)
简介 Runtime 又叫运行时,是一套底层的 C 语言 API,其为 iOS 内部的核心之一,我们平时编写的 OC 代码,底层都是基于它来实现的.比如: [receiver message]; // ...
随机推荐
- C和C++中的副本机制
函数的形参.return 都有副本机制.数组没有副本机制 (为了节约内存) 函数形参和局部变量的生命周期.函数调用结束后就会被回收.
- Vue.use源码分析(转)+如何封装一个组件
封装一个组件:https://www.jianshu.com/p/89a05706917a 我想有过vue开发经验的,对于vue.use并不陌生.当使用vue-resource或vue-router等 ...
- Updatexml函数再mysql中的作用
函数的解释 http://www.blogjava.net/chenpengyi/archive/2006/07/11/57578.html 我的理解就是updatexml函数具有查询功能 并且会再x ...
- java 用redisTemplate 的 Operations存取list集合
一 .存取为list类型 @RestController @RequestMapping("/test") @Slf4j public class TestController { ...
- AAAI 2019 分析
AAAI 2019 分析 Google Scholar 订阅 CoKE : Word Sense Induction Using Contextualized Knowledge Embeddings ...
- 【工具安装】BurpSuite 安装教程
日期:2019-07-14 17:23:53 介绍:安装 JDK,配置 JDK 的环境变量.安装 BurpSuite,抓包 0x01. 安装 JDK 安装 JDK BurpSuite 需要 JAVA ...
- Delphi IDE使用的一些主要技巧
Delphi IDE使用的一些主要技巧 1.查找和替换 (1)<ctrl>+F[1]:选择页“Find”,进行查找,则根据查找方向继续查找.选择页“Findin Files”,则进行该工程 ...
- C 语言中的预处理
C 语言中以 # 开头的就是预处理指令,例如 #include . 预处理指令的用途 所有的预处理指令都会在 GCC 编译过程的预处理步骤解析执行,替换为对应的内容.在下一步编译过程中,看不到任何预处 ...
- PHP is_file() 函数
is_file() 函数检查指定的文件名是否是正常的文件. 语法is_file(file)参数 描述file 必需.规定要检查的文件.说明如果文件存在且为正常的文件,则返回 true. 提示和注释 注 ...
- 来自数据源的 String 类型的给定值不能转换为指定目标列的类型 nvarchar
.TrimEnd() 怀疑是否SqlBulkCopy是否存在某种bug,故而在系统中改写代码,用单个sql的插入数据方式,用循环逐条导入.结果是没问题.难道真的是SqlBulkCopy有某种bug?上 ...