1.概述
UIPasteboard是ios中访问粘贴板的原生控件,可分为系统等级的和app等级的,系统等级的独立于app,可以复制一个app的内容到另一个app;app等级的只能在app内进行复制和粘贴;它们分别由+ (UIPasteboard )generalPasteboard;和*+ (nullable UIPasteboard )pasteboardWithName:(NSString )pasteboardName create:(BOOL)create;这两个类方法进行创建。
//获取系统级别的剪切板
+ (UIPasteboard *)generalPasteboard;
//获取一个自定义的剪切板 name参数为此剪切板的名称 create参数用于设置当这个剪切板不存在时 是否进行创建
+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create;
//获取一个应用内可用的剪切板
+ (UIPasteboard *)pasteboardWithUniqueName;
2.数据类型
可以复制在粘贴板的数据类型有NSString、UIImage、NSURL、UIColor、NSData以及由这些类型元素组成的数组。可分别由它们的set方法将数据放在粘贴板中,如NSString:
UIPasteboard pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setString:@"复制的字符串内容"];*
3. UIKit framework提供了几个类和协议方便我们在自己的应用程序中实现剪贴板的功能。
1、UIPasteboard:我们可以向其中写入数据,也可以读取数据
2、UIMenuController:显示一个快捷菜单,用来展示复制、剪贴、粘贴等选择的项。
3、UIResponder中的 canPerformAction:withSender:用于控制哪些命令显示在快捷菜单中。
4、当快捷菜单上的命令点击的时候,UIResponderStandardEditActions将会被调用。
4.认识常用方法
/*通过名称获取粘贴板并且移除*/
+ (void)removePasteboardWithName:(NSString *)pasteboardName;
/*从粘贴板中获取数据,pasteboardType是自定义的,说明app可以处理哪种类型的数据*/
- (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType;
/*data类型的数据放在粘贴板中,pasteboardType同上*/
- (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType;
/*从粘贴板中取出data*/
- (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType;
5.使用方法
在ios中,支持UIPasteboard原生控件只有UITextField 、UITextView、UIWebView这三个。如果想自定义一个控件能够使用UIPasteboard,需要在定义的时候重载
-(BOOL)canBecomeFirstResponder和 -(BOOL)canPerformAction:(SEL)action withSender:(id)sender
这两个方法,前者设置控件能接收到事件(能成为第一响应者),后者决定这个控件能够使用复制、剪切、选中、全选、粘贴等的哪一种或几种功能,并重载对应的
-(void)copy:(id)sender
-(void)cut:(id)sender
-(void)select:(id)sender
-(void)selectAll:(id)sender
-(void)paste:(id)sender
方法,在这几个方法中处理事件,UIMenuController负责显示UI。
6.代码演示:
- ViewController.m类:
#import "ViewController.h"
#import "CopyView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CopyView *cv = [[CopyView alloc] initWithFrame:self.view.bounds];
self.view = cv;
}
@end
- CopyView.m类
#import "CopyView.h"
@interface CopyView ()
@property (strong, nonatomic) UIImageView* img1;
@property (strong, nonatomic) UIImageView* img2;
@end
@implementation CopyView
// 懒加载创建img1
-(UIImageView *)img1{
if (_img1 == nil) {
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 100.0f, 100.0f)];
NSString* path = [[NSBundle mainBundle] pathForResource:@"Origin" ofType:@"jpg"];
_img1.image = [UIImage imageWithContentsOfFile:path];
}
return _img1;
}
// 懒加载创建img2
-(UIImageView *)img2{
if (_img2 == nil) {
_img2 = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.img1.frame)+50.0f, 20.0f, 100.0f, 100.0f)];
_img2.backgroundColor = [UIColor lightGrayColor];
}
return _img2;
}
// 初始化控件
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.backgroundColor = [UIColor whiteColor];
[self addSubview:self.img1];
[self addSubview:self.img2];
}
return self;
}
// img1可以成为第一响应器
-(BOOL)canBecomeFirstResponder{
return YES;
}
// img1能够使用复制、剪切、选中、全选、粘贴等的哪一种或几种功能
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender{
NSArray* methodNameArr = @[@"copy:",@"cut:",@"select:",@"selectAll:",@"paste:"];
if ([methodNameArr containsObject:NSStringFromSelector(action)]) {
return YES;
}
return [super canPerformAction:action withSender:sender];
}
// 实现复制功能(完成对指定控件的内容进行复制 ,即读数据)
-(void)copy:(id)sender{
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
// 复制img1
[pasteboard setImage:self.img1.image];
}
// 实现粘贴功能(完成粘贴到哪个控件上,即写数据)
-(void)paste:(id)sender{
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
// 粘贴到img2上
self.img2.image = [pasteboard image];
}
// 点击屏幕就为img1创建菜单
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self becomeFirstResponder];
// 创建菜单
UIMenuController* menuController = [UIMenuController sharedMenuController];
// 为img1设置菜单
[menuController setTargetRect:self.img1.frame inView:self];
// 在img1上显示菜单
[menuController setMenuVisible:YES animated:YES];
}
@end
7.效果演示:
文/charlesLaw(简书作者)原文链接:http://www.jianshu.com/p/1213f9f00fdd
参考资料:http://blog.csdn.net/likendsl/article/details/8955035
参考资料:http://www.jb51.net/article/86862.htm
参考资料:http://www.it165.net/pro/html/201407/16722.html