UIPasteboard 的使用方式:(粘贴、复制类的使用方法)
2016-11-01 11:28
841 查看
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
相关文章推荐
- ubuntu下使用codeblocks编程运行时终端不能复制粘贴的解决方法
- Android异步更新UI的方式之使用runOnUiThread(action)方法
- vim 下使用复制、剪切、粘贴命令的方法
- ubuntu下使用codeblocks编程运行时终端不能复制粘贴的解决方法
- Altium Designer复制粘贴的方法,以及特殊粘贴的使用
- UIMenuController/UIPasteboard(1) 制作一个可以粘贴复制的Label
- 使用Asp.net Mvc3 Razor视图方式扩展JQuery UI Widgets方法介绍
- UIMenuController UIPasteboard:复制粘贴详解
- 使用robot封装一个模拟键盘复制粘贴并按下回车的方法
- Element UI 中可展开表格手风琴效果实现(toggleRowExpansion方法的正确使用方式)
- window下使用直接复制数据库文件方式复制数据库和表的方法
- Android异步更新UI的方式之使用Handler的post(Runnabel r)方法
- Asp.net网页中禁止使用剪切、复制、粘贴的方法
- ubuntu下使用codeblocks编程运行时终端不能复制粘贴的解决方法
- Vim的粘贴、复制、删除使用方法
- 使用rman nocatalog方式备份Oracle - NetBackup配置方法
- ecliipse下安装插件的三种方法&amp;amp;使用links方式安装Eclipse插件
- 深度复制一个对象方法。利用反射机制,不使用Serialization,也不使用ICloneable接口
- word2007的问题 此错误通常是由宏安全性设置造成的。如果您知道宏来自您信任的来源,则可将宏安全性设置更改为允许启用宏。宏安全性设置的更改方式取决于您使用的 Microsoft Office System 程序 解决方法