iOS中管理剪切板的UIPasteboard粘貼板類用法詳解
一、自帶剪切板操作的原生UI控件
在iOS的UI系統(tǒng)中,有3個(gè)控件自帶剪切板操作,分別是UITextField、UITextView與UIWebView。在這些控件的文字交互處進(jìn)行長(zhǎng)按手勢(shì)可以在屏幕視圖上喚出系統(tǒng)的剪切板控件,用戶可以進(jìn)行復(fù)制、粘貼,剪切等操作,其效果分別如下圖所示。
UITextField的文字操作
UITextView的文字操作
二、系統(tǒng)的剪切板管理類UIPasteboard
實(shí)際上,當(dāng)用戶通過(guò)上面的空間進(jìn)行復(fù)制、剪切等操作時(shí),被選中的內(nèi)容會(huì)被存放到系統(tǒng)的剪切板中,并且這個(gè)剪切板并不只能存放字符串?dāng)?shù)據(jù),其還可以進(jìn)行圖片數(shù)據(jù)與網(wǎng)址URL數(shù)據(jù)的存放。這個(gè)剪切板就是UIPasteboard類,開(kāi)發(fā)者也可以直接通過(guò)它來(lái)操作數(shù)據(jù)進(jìn)行應(yīng)用內(nèi)或應(yīng)用間傳值。
UIPasteboard類有3個(gè)初始化方法,如下:
//獲取系統(tǒng)級(jí)別的剪切板 + (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è)級(jí)別不同的剪切板,系統(tǒng)級(jí)別的剪切板在整個(gè)設(shè)備中共享,即是應(yīng)用程序被刪掉,其向系統(tǒng)級(jí)的剪切板中寫入的數(shù)據(jù)依然在。自定義的剪切板通過(guò)一個(gè)特定的名稱字符串進(jìn)行創(chuàng)建,它在應(yīng)用程序內(nèi)或者同一開(kāi)發(fā)者開(kāi)發(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)級(jí)別的剪切板只有當(dāng)設(shè)備重新啟動(dòng)時(shí) 這個(gè)值才會(huì)清零 @property(readonly,nonatomic) NSInteger changeCount;
下面這些方法用于設(shè)置與獲取剪切板中的數(shù)據(jù):
最新一組數(shù)據(jù)對(duì)象的存取:
//獲取剪切板中最新數(shù)據(jù)的類型 - (NSArray<NSString *> *)pasteboardTypes; //獲取剪切板中最新數(shù)據(jù)對(duì)象是否包含某一類型的數(shù)據(jù) - (BOOL)containsPasteboardTypes:(NSArray<NSString *> *)pasteboardTypes; //將剪切板中最新數(shù)據(jù)對(duì)象某一類型的數(shù)據(jù)取出 - (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType; //將剪切板中最新數(shù)據(jù)對(duì)象某一類型的值取出 - (nullable id)valueForPasteboardType:(NSString *)pasteboardType; //為剪切板中最新數(shù)據(jù)對(duì)應(yīng)的某一數(shù)據(jù)類型設(shè)置值 - (void)setValue:(id)value forPasteboardType:(NSString *)pasteboardType; //為剪切板中最新數(shù)據(jù)對(duì)應(yīng)的某一數(shù)據(jù)類型設(shè)置數(shù)據(jù) - (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType; 多組數(shù)據(jù)對(duì)象的存?。? //數(shù)據(jù)組數(shù) @property(readonly,nonatomic) NSInteger numberOfItems; //獲取一組數(shù)據(jù)對(duì)象包含的數(shù)據(jù)類型 - (nullable NSArray *)pasteboardTypesForItemSet:(nullable NSIndexSet*)itemSet; //獲取一組數(shù)據(jù)對(duì)象中是否包含某些數(shù)據(jù)類型 - (BOOL)containsPasteboardTypes:(NSArray<NSString *> *)pasteboardTypes inItemSet:(nullable NSIndexSet *)itemSet; //根據(jù)數(shù)據(jù)類型獲取一組數(shù)據(jù)對(duì)象 - (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ù)對(duì)象 @property(nonatomic,copy) NSArray *items; //添加一組數(shù)據(jù)對(duì)象 - (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ū)ο?,在開(kāi)發(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; 對(duì)剪切板的某些操作會(huì)觸發(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ù)制圖片的簡(jiǎn)單例子
創(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)文章
iOS中l(wèi)ebel特殊字符的自動(dòng)換行問(wèn)題解決
這篇文章主要給大家介紹了關(guān)于iOS中l(wèi)ebel特殊字符的實(shí)現(xiàn)不自動(dòng)換行的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)iOS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-10-10使用objc runtime實(shí)現(xiàn)iOS閉環(huán)的懶加載功能
利用objc runtime的動(dòng)態(tài)性實(shí)現(xiàn)懶加載可以實(shí)現(xiàn)即可增加又可刪除功能,也可以避免污染類型。這篇文章主要介紹了使用objc runtime實(shí)現(xiàn)iOS閉環(huán)的懶加載功能,需要的朋友可以參考下2019-06-06深入講解iOS開(kāi)發(fā)中應(yīng)用數(shù)據(jù)的存儲(chǔ)方式
這篇文章主要介紹了iOS開(kāi)發(fā)中應(yīng)用數(shù)據(jù)的存儲(chǔ)方式,包括plistXML屬性列表和NSKeydeArchiver歸檔兩個(gè)部分,需要的朋友可以參考下2015-12-12iOS 獲取當(dāng)前的ViewController的方法
本篇文章主要介紹了iOS 獲取當(dāng)前的ViewController的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09iOS9蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議的方法
這篇文章主要介紹了iOS9蘋果將原h(huán)ttp協(xié)議改成了https協(xié)議的方法的相關(guān)資料,需要的朋友可以參考下2016-01-01ios swift3.0實(shí)現(xiàn)二維碼掃描、生成、識(shí)別示例代碼
本篇文章主要介紹了ios swift3.0實(shí)現(xiàn)二維碼掃描、生成、識(shí)別示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02