在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.lxweimin.com/p/a6d2e46329f8