UIPasteboard (粘贴板、剪切板使用)
来源:互联网 发布:湘北vs山王数据 编辑:程序博客网 时间:2024/05/22 04:12
static NSString *pasteBoardName = @"text.Board";@interface ViewController (){ UIButton *imageBtn; NSUInteger index; UIButton *pasteView;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; UIFont *fontSize = [UIFont boldSystemFontOfSize:22]; imageBtn = [[UIButton alloc]initWithFrame:CGRectMake(120, 40, 80, 80)]; [imageBtn setTitle:@"shareLine" forState:UIControlStateNormal]; [imageBtn setBackgroundImage:[UIImage imageNamed:@"shareLine"] forState:UIControlStateNormal]; [self.view addSubview:imageBtn]; UIButton *btn1 = [[UIButton alloc]initWithFrame:CGRectMake(120, 150, 100, 35)]; [btn1 setTitle:@"复制图片" forState:UIControlStateNormal]; [btn1 setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal]; btn1.tag = 1; btn1.titleLabel.font = fontSize; [btn1 setBackgroundColor:[UIColor lightGrayColor]]; [btn1 addTarget:self action:@selector(copyImg:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn1]; UIButton *btn2 = [[UIButton alloc]initWithFrame:CGRectMake(230, 150, 100, 35)]; [btn2 setTitle:@"复制文字" forState:UIControlStateNormal]; [btn2 setBackgroundColor:[UIColor lightGrayColor]]; btn2.tag = 2; [btn2 setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal]; btn2.titleLabel.font = fontSize; [btn2 addTarget:self action:@selector(copyImg:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn2]; UIButton *btn3 = [[UIButton alloc]initWithFrame:CGRectMake(10, 150, 100, 35)]; [btn3 setTitle:@"粘贴" forState:UIControlStateNormal]; btn3.titleLabel.font = fontSize; [btn3 setBackgroundColor:[UIColor lightGrayColor]]; [btn3 setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal]; [btn3 addTarget:self action:@selector(openActivity:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn3]; }#pragma mark#pragma mark - 复制-(void)copyImg:(UIButton*)btn{ UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:pasteBoardName create:YES]; switch (btn.tag) { case 1: /*方法1、转为data保存到剪切板 * @prame forPasteboardType public.JPEG / public.jpeg / public.png / public.tiff */ [appPasteBoard setData:UIImageJPEGRepresentation([UIImage imageNamed:@"shareLine"] , 1.0) forPasteboardType:@"public.JPEG"]; break; case 2: //复制文字 appPasteBoard.string = imageBtn.titleLabel.text; break; default: break; } index = btn.tag; }#pragma mark - 粘贴-(void)openActivity:(UIButton*)btn{ UIPasteboard *appPasteBoard = [UIPasteboard pasteboardWithName:pasteBoardName create:YES]; //获取当前剪切板数据类型数组 [pasteboardTypes] NSArray *ary = appPasteBoard.pasteboardTypes; //是否包含指定类型数据 BOOL b = [appPasteBoard containsPasteboardTypes:@[@"public.JPEG"]]; //获取数据类型数组 NSUInteger num = appPasteBoard.numberOfItems; //NSIndexSet NSIndexSet *set = [appPasteBoard itemSetWithPasteboardTypes:ary]; NSLog(@"%@",set); if (!pasteView) { pasteView = [[UIButton alloc]initWithFrame:CGRectMake(120, 220, 80, 80)]; [pasteView setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [self.view addSubview:pasteView]; } switch (index) { case 1: //方法1、获取剪切板 data类型 //@prame forPasteboardType 两边复制和粘贴约定好的类型字符串,可以任意 // NSData *data =[appPasteBoard dataForPasteboardType:@"public.jpeg"]; // [pasteView setImage:[UIImage imageWithData:data] forState:UIControlStateNormal]; //方法2、获取原图 [pasteView setTitle:nil forState:UIControlStateNormal]; [pasteView setImage:appPasteBoard.image forState:UIControlStateNormal]; break; case 2: [pasteView setImage:nil forState:UIControlStateNormal]; [pasteView setTitle:appPasteBoard.string forState:UIControlStateNormal]; break; default: break; } }
0 0
- UIPasteboard (粘贴板、剪切板使用)
- 精通UIPasteboard 粘贴板
- 精通UIPasteboard 粘贴板
- UIPasteboard 粘贴板
- 精通UIPasteboard 粘贴板
- 精通UIPasteboard 粘贴板
- 精通UIPasteboard 粘贴板
- 精通UIPasteboard 粘贴板
- UIPasteboard 粘贴板
- UIPasteboard 粘贴板
- 精通UIPasteboard粘贴板
- iOS 精通UIPasteboard 粘贴板
- IOS 剪切板(UIPasteboard)的使用
- ios 复制粘贴文本--UIPasteboard的使用
- [ios]精通UIPasteboard 粘贴板 【转】
- iOS——UIPasteboard 粘贴板
- iOS剪切板 UIPasteboard
- iOS剪切板 UIPasteboard
- fetch bulk collect into 批量效率的读取游标数据 【转载】
- Datatables中文API——基本参数
- JS资料整理
- 单例模式的七种写法
- HBase -ROOT-和.META.表结构
- UIPasteboard (粘贴板、剪切板使用)
- poj 3984 bfs+栈的使用
- ZooKeeper系列之二:Zookeeper常用命令
- 新闻api接口 关键字检索最新新闻动态
- winform应用程序更换皮肤
- C#生成DLL以及Delphi调用
- solrj之solr基本应用!
- python读写xml文件
- Android 框架