iOS中管理剪切板的UIPasteboard粘貼板類用法詳解
一、自帶剪切板操作的原生UI控件
在iOS的UI系統(tǒng)中,有3個(gè)控件自帶剪切板操作,分別是UITextField、UITextView與UIWebView。在這些控件的文字交互處進(jìn)行長按手勢可以在屏幕視圖上喚出系統(tǒng)的剪切板控件,用戶可以進(jìn)行復(fù)制、粘貼,剪切等操作,其效果分別如下圖所示。

UITextField的文字操作

UITextView的文字操作

二、系統(tǒng)的剪切板管理類UIPasteboard
實(shí)際上,當(dāng)用戶通過上面的空間進(jìn)行復(fù)制、剪切等操作時(shí),被選中的內(nèi)容會被存放到系統(tǒng)的剪切板中,并且這個(gè)剪切板并不只能存放字符串?dāng)?shù)據(jù),其還可以進(jìn)行圖片數(shù)據(jù)與網(wǎng)址URL數(shù)據(jù)的存放。這個(gè)剪切板就是UIPasteboard類,開發(fā)者也可以直接通過它來操作數(shù)據(jù)進(jìn)行應(yīng)用內(nèi)或應(yīng)用間傳值。
UIPasteboard類有3個(gè)初始化方法,如下:
//獲取系統(tǒng)級別的剪切板 + (UIPasteboard *)generalPasteboard; //獲取一個(gè)自定義的剪切板 name參數(shù)為此剪切板的名稱 create參數(shù)用于設(shè)置當(dāng)這個(gè)剪切板不存在時(shí) 是否進(jìn)行創(chuàng)建 + (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create; //獲取一個(gè)應(yīng)用內(nèi)可用的剪切板 + (UIPasteboard *)pasteboardWithUniqueName;
上面3個(gè)初始化方法,分別獲取或創(chuàng)建3個(gè)級別不同的剪切板,系統(tǒng)級別的剪切板在整個(gè)設(shè)備中共享,即是應(yīng)用程序被刪掉,其向系統(tǒng)級的剪切板中寫入的數(shù)據(jù)依然在。自定義的剪切板通過一個(gè)特定的名稱字符串進(jìn)行創(chuàng)建,它在應(yīng)用程序內(nèi)或者同一開發(fā)者開發(fā)的其他應(yīng)用程序中可以進(jìn)行數(shù)據(jù)共享。第3個(gè)方法創(chuàng)建的剪切板等價(jià)為使用第2個(gè)方法創(chuàng)建的剪切板,只是其名稱字符串為nil,它通常用于當(dāng)前應(yīng)用內(nèi)部。
注意:使用第3個(gè)方法創(chuàng)建的剪切板默認(rèn)是不進(jìn)行數(shù)據(jù)持久化的,及當(dāng)應(yīng)用程序退出后,剪切板中內(nèi)容將別抹去。若要實(shí)現(xiàn)持久化,需要設(shè)置persistent屬性為YES。
UIPasteboard中常用方法及屬性如下:
//剪切板的名稱 @property(readonly,nonatomic) NSString *name; //根據(jù)名稱刪除一個(gè)剪切板 + (void)removePasteboardWithName:(NSString *)pasteboardName; //是否進(jìn)行持久化 @property(getter=isPersistent,nonatomic) BOOL persistent; //此剪切板的改變次數(shù) 系統(tǒng)級別的剪切板只有當(dāng)設(shè)備重新啟動時(shí) 這個(gè)值才會清零 @property(readonly,nonatomic) NSInteger changeCount;
下面這些方法用于設(shè)置與獲取剪切板中的數(shù)據(jù):
最新一組數(shù)據(jù)對象的存?。?/p>
//獲取剪切板中最新數(shù)據(jù)的類型 - (NSArray<NSString *> *)pasteboardTypes; //獲取剪切板中最新數(shù)據(jù)對象是否包含某一類型的數(shù)據(jù) - (BOOL)containsPasteboardTypes:(NSArray<NSString *> *)pasteboardTypes; //將剪切板中最新數(shù)據(jù)對象某一類型的數(shù)據(jù)取出 - (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType; //將剪切板中最新數(shù)據(jù)對象某一類型的值取出 - (nullable id)valueForPasteboardType:(NSString *)pasteboardType; //為剪切板中最新數(shù)據(jù)對應(yīng)的某一數(shù)據(jù)類型設(shè)置值 - (void)setValue:(id)value forPasteboardType:(NSString *)pasteboardType; //為剪切板中最新數(shù)據(jù)對應(yīng)的某一數(shù)據(jù)類型設(shè)置數(shù)據(jù) - (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType; 多組數(shù)據(jù)對象的存取: //數(shù)據(jù)組數(shù) @property(readonly,nonatomic) NSInteger numberOfItems; //獲取一組數(shù)據(jù)對象包含的數(shù)據(jù)類型 - (nullable NSArray *)pasteboardTypesForItemSet:(nullable NSIndexSet*)itemSet; //獲取一組數(shù)據(jù)對象中是否包含某些數(shù)據(jù)類型 - (BOOL)containsPasteboardTypes:(NSArray<NSString *> *)pasteboardTypes inItemSet:(nullable NSIndexSet *)itemSet; //根據(jù)數(shù)據(jù)類型獲取一組數(shù)據(jù)對象 - (nullable NSIndexSet *)itemSetWithPasteboardTypes:(NSArray *)pasteboardTypes; //根據(jù)數(shù)據(jù)類型獲取一組數(shù)據(jù)的值 - (nullable NSArray *)valuesForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet; //根據(jù)數(shù)據(jù)類型獲取一組數(shù)據(jù)的NSData數(shù)據(jù) - (nullable NSArray *)dataForPasteboardType:(NSString *)pasteboardType inItemSet:(nullable NSIndexSet *)itemSet; //所有數(shù)據(jù)對象 @property(nonatomic,copy) NSArray *items; //添加一組數(shù)據(jù)對象 - (void)addItems:(NSArray<NSDictionary<NSString *, id> *> *)items;
上面方法中很多需要傳入數(shù)據(jù)類型參數(shù),這些參數(shù)是系統(tǒng)定義好的一些字符竄,如下:
//所有字符串類型數(shù)據(jù)的類型定義字符串?dāng)?shù)組 UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListString; //所有URL類型數(shù)據(jù)的類型定義字符串?dāng)?shù)組 UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListURL; //所有圖片數(shù)據(jù)的類型定義字符串?dāng)?shù)據(jù) UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListImage; //所有顏色數(shù)據(jù)的類型定義字符串?dāng)?shù)組 UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListColor;
相比于上面兩組方法,下面這些方法更加面向?qū)ο?,在開發(fā)中使用更加方便與快捷:
//獲取或設(shè)置剪切板中的字符串?dāng)?shù)據(jù) @property(nullable,nonatomic,copy) NSString *string; //獲取或設(shè)置剪切板中的字符串?dāng)?shù)組 @property(nullable,nonatomic,copy) NSArray<NSString *> *strings; //獲取或設(shè)置剪切板中的URL數(shù)據(jù) @property(nullable,nonatomic,copy) NSURL *URL; //獲取或設(shè)置剪切板中的URL數(shù)組 @property(nullable,nonatomic,copy) NSArray<NSURL *> *URLs; //獲取或s何止剪切板中的圖片數(shù)據(jù) @property(nullable,nonatomic,copy) UIImage *image; //獲取或設(shè)置剪切板中的圖片數(shù)組 @property(nullable,nonatomic,copy) NSArray<UIImage *> *images; //獲取或設(shè)置剪切板中的顏色數(shù)據(jù) @property(nullable,nonatomic,copy) UIColor *color; //獲取或設(shè)置剪切板中的顏色數(shù)組 @property(nullable,nonatomic,copy) NSArray<UIColor *> *colors; 對剪切板的某些操作會觸發(fā)如下通知: //剪切板內(nèi)容發(fā)生變化時(shí)發(fā)送的通知 UIKIT_EXTERN NSString *const UIPasteboardChangedNotification; //剪切板數(shù)據(jù)類型鍵值增加時(shí)發(fā)送的通知 UIKIT_EXTERN NSString *const UIPasteboardChangedTypesAddedKey; //剪切板數(shù)據(jù)類型鍵值移除時(shí)發(fā)送的通知 UIKIT_EXTERN NSString *const UIPasteboardChangedTypesRemovedKey; //剪切板被刪除時(shí)發(fā)送的通知 UIKIT_EXTERN NSString *const UIPasteboardRemovedNotification;
三、復(fù)制圖片的簡單例子
創(chuàng)建一個(gè)CopyView
#import "CopyView.h"
@interface CopyView ()
@property (strong, nonatomic) UIImageView* img1;
@property (strong, nonatomic) UIImageView* img2;
@end
@implementation CopyView
-(UIImageView *)img1{
if (_img1 == nil) {
_img1 = [[UIImageView alloc] initWithFrame:CGRectMake(10.0f, 20.0f, 100.0f, 100.0f)];
NSString* path = [[NSBundle mainBundle] pathForResource:@"NetworldImage" ofType:@"jpg"];
_img1.image = [UIImage imageWithContentsOfFile:path];
}
return _img1;
}
-(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;
}
-(BOOL)canBecomeFirstResponder{
return YES;
}
-(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];
[pasteboard setImage:self.img1.image];
}
-(void)paste:(id)sender{
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
self.img2.image = [pasteboard image];
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self becomeFirstResponder];
UIMenuController* menuController = [UIMenuController sharedMenuController];
[menuController setTargetRect:self.img1.frame inView:self];
[menuController setMenuVisible:YES animated:YES];
}
@end
在controller中
#import "ViewController.h"
#import "CopyView.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
CopyView* cv = [[CopyView alloc] initWithFrame:self.view.bounds];
self.view = cv;
}
@end
效果展示


相關(guān)文章
Objective-C中關(guān)于實(shí)例所占內(nèi)存的大小詳解
這篇文章主要給大家介紹了關(guān)于Objective-C中實(shí)例所占內(nèi)存的大小的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對各位iOS開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05
微信小程序 實(shí)現(xiàn)listview帶字母滑動
這篇文章主要介紹了微信小程序 實(shí)現(xiàn)listview帶字母滑動的相關(guān)資料,需要的朋友可以參考下2017-05-05
iOS表視圖之下拉刷新控件功能的實(shí)現(xiàn)方法
下拉刷新是重新刷新表視圖或列表,以便重新加載數(shù)據(jù),這種模式廣泛用于移動平臺,相信大家對于此也是非常熟悉的,那么iOS是如何做到的下拉刷新呢?下面小編給大家分享iOS表視圖之下拉刷新控件的實(shí)現(xiàn)方法,一起看看吧2017-01-01
iOS mobileconfig配置文件進(jìn)行簽名的配置方法
這篇文章主要介紹了iOS mobileconfig配置文件進(jìn)行簽名的配置方法,給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02

