1.概述
UIPasteboard是ios中訪問粘貼板的原生控件,可分為系統等級的和app等級的,系統等級的獨立於app,可以復制一個app的內容到另一個app;app等級的只能在app內進行復制和粘貼;它們分別由+ (nullable UIPasteboard *)pasteboardWithName:(NSString *)pasteboardName create:(BOOL)create
這兩個類方法進行創建。
2.數據類型
可以復制在粘貼板的數據類型有NSString、UIImage、NSURL、UIColor、NSData以及由這些類型元素組成的數組。可分別由它們的set方法將數據放在粘貼板中,如NSString:
UIPasteboard* pasteboard = [UIPasteboard generalPasteboard]; [pasteboard setString:@"復制的字符串內容"];
3.認識常用方法
/*通過名稱獲取粘貼板並且移除*/ + (void)removePasteboardWithName:(NSString *)pasteboardName; /*從粘貼板中獲取數據,pasteboardType是自定義的,說明app可以處理哪種類型的數據*/ - (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType; /*data類型的數據放在粘貼板中,pasteboardType同上*/ - (void)setData:(NSData *)data forPasteboardType:(NSString *)pasteboardType; /*從粘貼板中取出data*/ - (nullable NSData *)dataForPasteboardType:(NSString *)pasteboardType;
4.使用方法
在ios中,支持UIPasteboard原生控件只有UITextField 、UITextView、UIWebView這三個,如果想自定義一個控件能夠使用UIPasteboard,需要在定義的時候重載-(BOOL)canBecomeFirstResponder
和 -(BOOL)canPerformAction:(SEL)action withSender:(id)sender
這兩個方法,前者設置控件可稱為第一響應器,后者決定這個控件能夠使用復制、剪切、選中、全選、粘貼等的哪一種或幾種功能,並重載對應的-(void)copy:(id)sender
、-(void)cut:(id)sender
、-(void)select:(id)sender
、-(void)selectAll:(id)sender
、-(void)paste:(id)sender
方法,在這幾個方法中處理事件,UIMenuController負責顯示UI。
5.復制圖片的簡單例子
創建一個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
原文鏈接:http://www.jianshu.com/p/1213f9f00fdd