1、自帶剪切板操作的原生UI控件
在IOS的UI體系中,有3個控件自帶剪切板操作,分離是UITextField、UITextView與UIWebView。在這些控件的文字交互處停止長按手勢可以在屏幕視圖上喚出體系的剪切板控件,用戶可以停止復制、粘貼,剪切等操作,其後果分離以下圖所示。

UITextField的文字操作

UITextView的文字操作

2、體系的剪切板治理類UIPasteboard
現實上,當用戶經由過程下面的空間停止復制、剪切等操作時,被選中的內容會被寄存到體系的剪切板中,而且這個剪切板其實不只能寄存字符串數據,其還可以停止圖片數據與網址URL數據的寄存。這個剪切板就是UIPasteboard類,開辟者也能夠直接經由過程它來操作數據停止運用內或運用間傳值。
UIPasteboard類有3個初始化辦法,以下:
//獲得體系級其余剪切板 + (UIPasteboard *)generalPasteboard; //獲得一個自界說的剪切板 name參數為此剪切板的稱號 create參數用於設置當這個剪切板不存在時 能否停止創立 + (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create; //獲得一個運用內可用的剪切板 + (UIPasteboard *)pasteboardWithUniqueName;
下面3個初始化辦法,分離獲得或創立3個級別分歧的剪切板,體系級其余剪切板在全部裝備中同享,等於運用法式被刪失落,其向體系級的剪切板中寫入的數據仍然在。自界說的剪切板經由過程一個特定的稱號字符串停止創立,它在運用法式內或許統一開辟者開辟的其他運用法式中可以停止數據同享。第3個辦法創立的剪切板等價為應用第2個辦法創立的剪切板,只是其稱號字符串為nil,它平日用於以後運用外部。
留意:應用第3個辦法創立的剪切板默許是不停止數據耐久化的,及當運用法式加入後,剪切板中內容將別抹去。若要完成耐久化,須要設置persistent屬性為YES。
UIPasteboard中經常使用辦法及屬性以下:
//剪切板的稱號 @property(readonly,nonatomic) NSString *name; //依據稱號刪除一個剪切板 + (void)removePasteboardWithName:(NSString *)pasteboardName; //能否停止耐久化 @property(getter=isPersistent,nonatomic) BOOL persistent; //此剪切板的轉變次數 體系級其余剪切板只要當裝備從新啟動時 這個值才會清零 @property(readonly,nonatomic) NSInteger changeCount;
上面這些辦法用於設置與獲得剪切板中的數據:
最新一組數據對象的存取:
//獲得剪切板中最新數據的類型 - (NSArray<NSString *> *)pasteboardTypes; //獲得剪切板中最新數據對象能否包括某一類型的數據 - (BOOL)containsPasteboardTypes:(NSArray<NSString *> *)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<NSString *> *)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<NSDictionary<NSString *, id> *> *)items;
下面辦法中許多須要傳入數據類型參數,這些參數是體系界說好的一些字符竄,以下:
//一切字符串類型數據的類型界說字符串數組 UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListString; //一切URL類型數據的類型界說字符串數組 UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListURL; //一切圖片數據的類型界說字符串數據 UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListImage; //一切色彩數據的類型界說字符串數組 UIKIT_EXTERN NSArray<NSString *> *UIPasteboardTypeListColor;
比擬於下面兩組辦法,上面這些辦法加倍面向對象,在開辟中應用加倍便利與快捷:
//獲得或設置剪切板中的字符串數據 @property(nullable,nonatomic,copy) NSString *string; //獲得或設置剪切板中的字符串數組 @property(nullable,nonatomic,copy) NSArray<NSString *> *strings; //獲得或設置剪切板中的URL數據 @property(nullable,nonatomic,copy) NSURL *URL; //獲得或設置剪切板中的URL數組 @property(nullable,nonatomic,copy) NSArray<NSURL *> *URLs; //獲得或s何止剪切板中的圖片數據 @property(nullable,nonatomic,copy) UIImage *image; //獲得或設置剪切板中的圖片數組 @property(nullable,nonatomic,copy) NSArray<UIImage *> *images; //獲得或設置剪切板中的色彩數據 @property(nullable,nonatomic,copy) UIColor *color; //獲得或設置剪切板中的色彩數組 @property(nullable,nonatomic,copy) NSArray<UIColor *> *colors; 對剪切板的某些操作會觸發以下告訴: //剪切板內容產生變更時發送的告訴 UIKIT_EXTERN NSString *const UIPasteboardChangedNotification; //剪切板數據類型鍵值增長時發送的告訴 UIKIT_EXTERN NSString *const UIPasteboardChangedTypesAddedKey; //剪切板數據類型鍵值移除時發送的告訴 UIKIT_EXTERN NSString *const UIPasteboardChangedTypesRemovedKey; //剪切板被刪除時發送的告訴 UIKIT_EXTERN NSString *const UIPasteboardRemovedNotification;
3、復制圖片的簡略例子
創立一個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
後果展現


【iOS中治理剪切板的UIPasteboard粘貼板類用法詳解】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!