iOS oc-剪切板

一、自帶剪切板操作的原生UI控件

在iOS的UI系統(tǒng)中,有3個控件自帶剪切板操作,分別是UITextField、UITextView與UIWebView。在這些控件的文字交互處進行長按手勢可以在屏幕視圖上喚出系統(tǒng)的剪切板控件,用戶可以進行復(fù)制、粘貼,剪切等操作


二、系統(tǒng)的剪切板管理類UIPasteboard

實際上,當(dāng)用戶通過上面的空間進行復(fù)制、剪切等操作時,被選中的內(nèi)容會被存放到系統(tǒng)的剪切板中,并且這個剪切板并不只能存放字符串?dāng)?shù)據(jù),其還可以進行圖片數(shù)據(jù)與網(wǎng)址URL數(shù)據(jù)的存放。這個剪切板就是UIPasteboard類,開發(fā)者也可以直接通過它來操作數(shù)據(jù)進行應(yīng)用內(nèi)或應(yīng)用間傳值。

UIPasteboard類有3個初始化方法,如下:


//獲取系統(tǒng)級別的剪切板

+ (UIPasteboard *)generalPasteboard;

//獲取一個自定義的剪切板 name參數(shù)為此剪切板的名稱 create參數(shù)用于設(shè)置當(dāng)這個剪切板不存在時 是否進行創(chuàng)建

+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create;

//獲取一個應(yīng)用內(nèi)可用的剪切板

+ (UIPasteboard *)pasteboardWithUniqueName;


上面3個初始化方法,分別獲取或創(chuàng)建3個級別不同的剪切板,系統(tǒng)級別的剪切板在整個設(shè)備中共享,即是應(yīng)用程序被刪掉,其向系統(tǒng)級的剪切板中寫入的數(shù)據(jù)依然在。自定義的剪切板通過一個特定的名稱字符串進行創(chuàng)建,它在應(yīng)用程序內(nèi)或者同一開發(fā)者開發(fā)的其他應(yīng)用程序中可以進行數(shù)據(jù)共享。第3個方法創(chuàng)建的剪切板等價為使用第2個方法創(chuàng)建的剪切板,只是其名稱字符串為nil,它通常用于當(dāng)前應(yīng)用內(nèi)部。

注意:使用第3個方法創(chuàng)建的剪切板默認是不進行數(shù)據(jù)持久化的,及當(dāng)應(yīng)用程序退出后,剪切板中內(nèi)容將別抹去。若要實現(xiàn)持久化,需要設(shè)置persistent屬性為YES。


UIPasteboard中常用方法及屬性如下:

//剪切板的名稱

@property(readonly,nonatomic) NSString *name;

//根據(jù)名稱刪除一個剪切板

+ (void)removePasteboardWithName:(NSString *)pasteboardName;

//是否進行持久化

@property(getter=isPersistent,nonatomic) BOOL persistent;

//此剪切板的改變次數(shù) 系統(tǒng)級別的剪切板只有當(dāng)設(shè)備重新啟動時 這個值才會清零

@property(readonly,nonatomic) NSInteger changeCount;


下面這些方法用于設(shè)置與獲取剪切板中的數(shù)據(jù):

最新一組數(shù)據(jù)對象的存取:


/獲取剪切板中最新數(shù)據(jù)的類型

- (NSArray*)pasteboardTypes;

//獲取剪切板中最新數(shù)據(jù)對象是否包含某一類型的數(shù)據(jù)- (BOOL)containsPasteboardTypes:(NSArray*)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ù)對象的存?。?/p>

?//數(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*)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*> *)items;


上面方法中很多需要傳入數(shù)據(jù)類型參數(shù),這些參數(shù)是系統(tǒng)定義好的一些字符竄,如下:


//所有字符串類型數(shù)據(jù)的類型定義字符串?dāng)?shù)組

UIKIT_EXTERN NSArray*UIPasteboardTypeListString;

//所有URL類型數(shù)據(jù)的類型定義字符串?dāng)?shù)組

UIKIT_EXTERN NSArray*UIPasteboardTypeListURL;

//所有圖片數(shù)據(jù)的類型定義字符串?dāng)?shù)據(jù)

UIKIT_EXTERN NSArray*UIPasteboardTypeListImage;

//所有顏色數(shù)據(jù)的類型定義字符串?dāng)?shù)組

UIKIT_EXTERN NSArray*UIPasteboardTypeListColor;


相比于上面兩組方法,下面這些方法更加面向?qū)ο?,在開發(fā)中使用更加方便與快捷:


//獲取或設(shè)置剪切板中的字符串?dāng)?shù)據(jù)

@property(nullable,nonatomic,copy) NSString *string;

//獲取或設(shè)置剪切板中的字符串?dāng)?shù)組

@property(nullable,nonatomic,copy) NSArray*strings;

//獲取或設(shè)置剪切板中的URL數(shù)據(jù)

@property(nullable,nonatomic,copy) NSURL *URL;

//獲取或設(shè)置剪切板中的URL數(shù)組

@property(nullable,nonatomic,copy) NSArray*URLs;

//獲取或s何止剪切板中的圖片數(shù)據(jù)

@property(nullable,nonatomic,copy) UIImage *image;

//獲取或設(shè)置剪切板中的圖片數(shù)組

@property(nullable,nonatomic,copy) NSArray*images;

//獲取或設(shè)置剪切板中的顏色數(shù)據(jù)

@property(nullable,nonatomic,copy) UIColor *color;

//獲取或設(shè)置剪切板中的顏色數(shù)組

@property(nullable,nonatomic,copy) NSArray*colors;


對剪切板的某些操作會觸發(fā)如下通知:


//剪切板內(nèi)容發(fā)生變化時發(fā)送的通知

UIKIT_EXTERN NSString *const UIPasteboardChangedNotification;

//剪切板數(shù)據(jù)類型鍵值增加時發(fā)送的通知

UIKIT_EXTERN NSString *const UIPasteboardChangedTypesAddedKey;

//剪切板數(shù)據(jù)類型鍵值移除時發(fā)送的通知

UIKIT_EXTERN NSString *const UIPasteboardChangedTypesRemovedKey;

//剪切板被刪除時發(fā)送的通知

UIKIT_EXTERN NSString *const UIPasteboardRemovedNotification;


三、復(fù)制圖片的簡單例子

創(chuàng)建一個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*)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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 在iOS中,UITextField、UITextView和UIWebView等都有復(fù)制粘貼等功能。而其她控件卻沒有...
    夏了夏天_feea閱讀 2,218評論 0 3
  • *7月8日上午 N:Block :跟一個函數(shù)塊差不多,會對里面所有的內(nèi)容的引用計數(shù)+1,想要解決就用__block...
    炙冰閱讀 2,711評論 1 14
  • 1. 利用 WKWebView 進行交互(系統(tǒng) API) 2. 利用 UIWebView 進行交互(系統(tǒng) API)...
    ElvisSun閱讀 1,029評論 0 1
  • 黃泉路,彼岸花,原先那火一般的妖冶之姿,如今,竟只剩孤葉成災(zāi)。 一襲紅衣的曼殊匆匆而來,推開門,便見一綠衫的男子站...
    月懶西樓閱讀 464評論 2 0
  • 本文內(nèi)容: 一.前兩天沒有寫作的原因; 二.近來總結(jié) 三.計劃調(diào)整方針 1 時光會檢驗一切,這是一個很殘酷的現(xiàn)實。...
    7515b237f6ce閱讀 496評論 0 0

友情鏈接更多精彩內(nèi)容