在iOS中,UITextField、UITextView和UIWebView等都有复制粘贴等功能。而其她控件却没有集成这些方便操作的功能。下面我将通过对粘贴板UIPasteboard这个类来详细说明在iOS中粘贴板的使用方法。
1、剪切板管理类UIPasteboard详解
UIPasteboard类有3个初始化方法,如下:
//获取系统级别的剪切板
+ (UIPasteboard *)generalPasteboard;
//获取一个自定义的剪切板 name参数为此剪切板的名称 create参数用于设置当这个剪切板不存在时 是否进行创建
+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create;
//获取一个应用内可用的剪切板
+ (UIPasteboard *)pasteboardWithUniqueName;
上面3个初始化方法,分别获取或创建3个级别不同的剪切板,下面我们详解一下在什么情况下用哪种初始化方法
+ (UIPasteboard *)generalPasteboard;系统级别的剪切板在整个设备中共享,即是应用程序被删掉,其向系统级的剪切板中写入的数据依然在。
+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create;自定义的剪切板通过一个特定的名称字符串进行创建,它在应用程序内或者同一开发者开发的其他应用程序中可以进行数据共享。举个例子:比如你开发了多款应用,用户全部下载了,在A应用中用户拷贝了一些数据(为了数据安全,不用系统级别的Pasteboard),在打开B应用时就会自动识别,提高用户体验。
+ (UIPasteboard *)pasteboardWithUniqueName;第3个方法创建的剪切板等价为使用第2个方法创建的剪切板,只是其名称字符串为nil,它通常用于当前应用内部。(当然也可以跨应用使用,但必须Bundle Identifier 例com.maoshaoqian.** 星号前部一样)
注意:使用第3个方法创建的剪切板默认是不进行数据持久化的,及当应用程序退出后,剪切板中内容将别抹去。若要实现持久化,需要设置persistent属性为YES。
下面我们来看一下UIPasteboard的常用属性
//剪切板的名称
@property(readonly,nonatomic) NSString *name;
//根据名称删除一个剪切板
+ (void)removePasteboardWithName:(NSString *)pasteboardName;
//是否进行持久化
@property(getter=isPersistent,nonatomic) BOOL persistent;
//此剪切板的改变次数 系统级别的剪切板只有当设备重新启动时 这个值才会清零
@property(readonly,nonatomic) NSInteger changeCount;
UIPasteboard数据类型判断及其存取
//获取剪切板中最新数据的类型
- (NSArray *)pasteboardTypes;
//获取剪切板中最新数据对象是否包含某一类型的数据
- (BOOL)containsPasteboardTypes:(NSArray *)pasteboardTypes;
//将剪切板中最新数据对象某一类型的数据取出
- (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType;
//将剪切板中最新数据对象某一类型的值取出
- (nullable id)valueForPasteboardType:(NSString *)pasteboardType;
//为剪切板中最新数据对应的某一数据类型设置值
- (void)setValue:(id)value forPasteboardType:(NSString *)pasteboardType;
//为剪切板中最新数据对应的某一数据类型设置数据
- (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType;
多组数据对象的存取:
//数据组数
@property(readonly,nonatomic) NSInteger numberOfItems;
//获取一组数据对象包含的数据类型
- (nullable NSArray *)pasteboardTypesForItemSet:(nullable NSIndexSet*)itemSet;
//获取一组数据对象中是否包含某些数据类型
- (BOOL)containsPasteboardTypes:(NSArray *)pasteboardTypes inItemSet:(nullable NSIndexSet *)itemSet;
//根据数据类型获取一组数据对象
- (nullable NSIndexSet *)itemSetWithPasteboardTypes:(NSArray *)pasteboardTypes;
//根据数据类型获取一组数据的值
- (nullable NSArray *)valuesForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet;
//根据数据类型获取一组数据的NSData数据
- (nullable NSArray *)dataForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet;
//所有数据对象
@property(nonatomic,copy) NSArray *items;
//添加一组数据对象
- (void)addItems:(NSArray *> *)items;
上面方法中很多需要传入数据类型参数,这些参数是系统定义好的一些字符窜,如下:
//所有字符串类型数据的类型定义字符串数组
UIKIT_EXTERN NSArray *UIPasteboardTypeListString;
//所有URL类型数据的类型定义字符串数组
UIKIT_EXTERN NSArray *UIPasteboardTypeListURL;
//所有图片数据的类型定义字符串数据
UIKIT_EXTERN NSArray *UIPasteboardTypeListImage;
//所有颜色数据的类型定义字符串数组
UIKIT_EXTERN NSArray *UIPasteboardTypeListColor;
相比于上面两组方法,下面这些方法更加面向对象,在开发中使用更加方便与快捷:
//获取或设置剪切板中的字符串数据
@property(nullable,nonatomic,copy) NSString *string;
//获取或设置剪切板中的字符串数组
@property(nullable,nonatomic,copy) NSArray *strings;
//获取或设置剪切板中的URL数据
@property(nullable,nonatomic,copy) NSURL *URL;
//获取或设置剪切板中的URL数组
@property(nullable,nonatomic,copy) NSArray *URLs;
//获取或s何止剪切板中的图片数据
@property(nullable,nonatomic,copy) UIImage *image;
//获取或设置剪切板中的图片数组
@property(nullable,nonatomic,copy) NSArray *images;
//获取或设置剪切板中的颜色数据
@property(nullable,nonatomic,copy) UIColor *color;
//获取或设置剪切板中的颜色数组
@property(nullable,nonatomic,copy) NSArray *colors;
//部分代码参考
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
//action 会返回很多,想用哪个就写那个(action == @selector(cut:) )
return (action == @selector(copy:) || action == @selector(paste:) );
}
-(void)copy:(id)sender{
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setImage:self.image];
if ([self.delegate respondsToSelector:@selector(transSomeTing:)]) {
[self.delegate transSomeTing:pasteboard.image];
NSLog(@"%@",self.image);
}
NSLog(@"您点击的是拷贝%@",pasteboard.items);
}
-(void)paste:(id)sender{
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
UIImage *image = [pasteboard image];
if (image) {
self.image = image;
}
NSLog(@"您点击的是粘贴");
}
- (void)cut:(id)sender {
UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setImage:self.image];
NSLog(@"您点击的是剪切");
}
- (void)select:(id)sender {
NSLog(@"您点击的是选择");
}
-(void)selectAll:(id)sender {
NSLog(@"您点击的是全选");
}
对剪切板的某些操作会触发如下通知:
//剪切板内容发生变化时发送的通知
UIKIT_EXTERN NSString *const UIPasteboardChangedNotification;
//剪切板数据类型键值增加时发送的通知
UIKIT_EXTERN NSString *const UIPasteboardChangedTypesAddedKey;
//剪切板数据类型键值移除时发送的通知
UIKIT_EXTERN NSString *const UIPasteboardChangedTypesRemovedKey;
//剪切板被删除时发送的通知
UIKIT_EXTERN NSString *const UIPasteboardRemovedNotification;
//使用举例
//当剪切板被删除时,监听通知,可处理相应事件;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillHide) name:UIPasteboardRemovedNotification object:nil];
2、剪切板管理类UIPasteboard具体使用
我们以系统粘贴板+ (UIPasteboard *)generalPasteboard;来举例子
我们给UIImageView添加复制粘贴事件
//
// ViewController.m
// Practice_UIPasteboard
#import "ViewController.h"
#import "PasteboardLabel.h"
#import "PasteboardImageView.h"
#import
@interface ViewController ()
@property (strong, nonatomic) IBOutlet PasteboardImageView *leftImageView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.leftLabel.userInteractionEnabled = YES;
//用于监听 UIMenuController的变化
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillShow) name:UIMenuControllerWillShowMenuNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuControllerWillHide) name:UIMenuControllerWillHideMenuNotification object:nil];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)longPressGestureAction:(UILongPressGestureRecognizer *)sender {
//要将图片变为第一响应者,而且要把图片设为**可交换状态**
[self.leftImageView becomeFirstResponder];
self.leftImageView.userInteractionEnabled = YES;
self.leftImageView.delegate = self;
UIMenuController *menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:self.leftImageView.frame inView:self.view];
[menuController setMenuVisible:YES animated:YES];
}
}
// PasteboardImageView.m
// Practice_UIPasteboard
#import "PasteboardImageView.h"
@implementation PasteboardImageView
//这个方法不能少
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
return (action == @selector(copy:) || action == @selector(paste:) );
}
-(void)copy:(id)sender{
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
[pasteboard setImage:self.image];
if ([self.delegate respondsToSelector:@selector(transSomeTing:)]) {
[self.delegate transSomeTing:pasteboard.image];
NSLog(@"%@",self.image);
}
NSLog(@"您点击的是拷贝%@",pasteboard.items);
}
-(void)paste:(id)sender{
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
UIImage *image = [pasteboard image];
if (image) {
self.image = image;
}
NSLog(@"您点击的是粘贴");
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
@end
参考来自:作者山水域 http://www.jianshu.com/p/a6d2e46329f8