UIPasteboard 的使用方式:(粘贴、复制类的使用方法)
分类:
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
文/charlesLaw(简书作者)
原文链接:http://www.jianshu.com/p/1213f9f00fdd
原文链接:http://www.jianshu.com/p/1213f9f00fdd