Swift - UIPasteboard剪贴板的使用详解(复制、粘贴文字和图片)
转载自:http://www.hangge.com/blog/cache/detail_1085.html
UITextField、UITextView组件系统原生就支持文字的复制,但有时我们需要让其他的一些组件也能实现复制功能,比如点击复制UILabel上的文字、UIImageView中的图片、UITableView里单元格的内容、或者点击按钮把文字或图片自动复制到粘贴板中等等。
1
|
UIPasteboard .generalPasteboard().string = "欢迎访问 hangge.com" |
2,复制字符串数组
1
|
UIPasteboard .generalPasteboard().strings = [ "hellow" , "hangge.com" ] |
3,复制图片
1
2
|
let image = UIImage (named: "logo.png" ) UIPasteboard .generalPasteboard().image = image |
4,复制二进制数据(NSData)
1
2
3
|
let path = NSBundle .mainBundle().pathForResource( "logo" , ofType: "png" )! let fileData = NSData (contentsOfFile: path)! UIPasteboard .generalPasteboard().setData(fileData, forPasteboardType: "public.png" ) |
注:从剪贴板获取二进制数据(NSData)
1
|
let myData = UIPasteboard .generalPasteboard().dataForPasteboardType( "public.png" ) |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import UIKit class UICopyLabel : UILabel { override init (frame: CGRect ) { super . init (frame: frame) sharedInit() } required init ?(coder aDecoder: NSCoder ) { super . init (coder: aDecoder) sharedInit() } func sharedInit() { userInteractionEnabled = true addGestureRecognizer( UILongPressGestureRecognizer (target: self , action: "showMenu:" )) } func showMenu(sender: AnyObject ?) { becomeFirstResponder() let menu = UIMenuController .sharedMenuController() if !menu.menuVisible { menu.setTargetRect(bounds, inView: self ) menu.setMenuVisible( true , animated: true ) } } //复制 override func copy (sender: AnyObject ?) { let board = UIPasteboard .generalPasteboard() board.string = text let menu = UIMenuController .sharedMenuController() menu.setMenuVisible( false , animated: true ) } override func canBecomeFirstResponder() -> Bool { return true } override func canPerformAction(action: Selector , withSender sender: AnyObject ?) -> Bool { if action == "copy:" { return true } return false } } |
在这个文本标签上长按后便可以复制其内容:

我们自定义一个图片控件类 UICPImageView(继承UIImageView),内部同样添加Touch事件响应。该控件不仅支持复制,还支持粘贴。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
import UIKit class UICPImageView : UIImageView { override init (frame: CGRect ) { super . init (frame: frame) sharedInit() } required init ?(coder aDecoder: NSCoder ) { super . init (coder: aDecoder) sharedInit() } func sharedInit() { userInteractionEnabled = true addGestureRecognizer( UILongPressGestureRecognizer (target: self , action: "showMenu:" )) } func showMenu(sender: AnyObject ?) { becomeFirstResponder() let menu = UIMenuController .sharedMenuController() if !menu.menuVisible { menu.setTargetRect(bounds, inView: self ) menu.setMenuVisible( true , animated: true ) } } //复制 override func copy (sender: AnyObject ?) { let board = UIPasteboard .generalPasteboard() board.image = self .image let menu = UIMenuController .sharedMenuController() menu.setMenuVisible( false , animated: true ) } //粘贴 override func paste(sender: AnyObject ?) { let board = UIPasteboard .generalPasteboard() self .image = board.image let menu = UIMenuController .sharedMenuController() menu.setMenuVisible( false , animated: true ) } override func canBecomeFirstResponder() -> Bool { return true } override func canPerformAction(action: Selector , withSender sender: AnyObject ?) -> Bool { if action == "copy:" { return true } else if action == "paste:" { return true } return false } } |
下面我们在界面上添加两个 UICPImageView,我们可以把左边控件里的图片复制到右边控件中来,效果图如下:



3,让表格(UITableView)支持复制功能
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
import UIKit class ViewController : UIViewController , UITableViewDelegate , UITableViewDataSource { var tableView: UITableView ? var tableData = [ "条目1" , "条目2" , "条目3" , "条目4" , "条目5" , "条目6" , "条目7" ] override func loadView() { super .loadView() } override func viewDidLoad() { super .viewDidLoad() //创建表视图 self .tableView = UITableView (frame: self .view.frame, style:. Plain ) self .tableView!.delegate = self self .tableView!.dataSource = self //创建一个重用的单元格 self .tableView!.registerClass( UITableViewCell . self , forCellReuseIdentifier: "SwiftCell" ) self .view.addSubview( self .tableView!) } func tableView(tableView: UITableView , performAction action: Selector , forRowAtIndexPath indexPath: NSIndexPath , withSender sender: AnyObject ?) { let board = UIPasteboard .generalPasteboard() board.string = tableData[indexPath.row] } func tableView(tableView: UITableView , canPerformAction action: Selector , forRowAtIndexPath indexPath: NSIndexPath , withSender sender: AnyObject ?) -> Bool { if action == "copy:" { return true } return false } func tableView(tableView: UITableView , shouldShowMenuForRowAtIndexPath indexPath: NSIndexPath ) -> Bool { return true } //在本例中,只有一个分区 func numberOfSectionsInTableView(tableView: UITableView ) -> Int { return 1; } //返回表格行数(也就是返回控件数) func tableView(tableView: UITableView , numberOfRowsInSection section: Int ) -> Int { return tableData.count } //创建各单元显示内容(创建参数indexPath指定的单元) func tableView(tableView: UITableView , cellForRowAtIndexPath indexPath: NSIndexPath ) -> UITableViewCell { //为了提供表格显示性能,已创建完成的单元需重复使用 let identify: String = "SwiftCell" //同一形式的单元格重复使用,在声明时已注册 let cell = tableView.dequeueReusableCellWithIdentifier(identify, forIndexPath: indexPath) as UITableViewCell cell.accessoryType = UITableViewCellAccessoryType . DisclosureIndicator cell.textLabel?.text = tableData[indexPath.row] return cell } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() } } |
长按某个单元格即可复制这个单元格内容:

原文出自:www.hangge.com 转载请保留原文链接:http://www.hangge.com/blog/cache/detail_1085.html
Swift - UIPasteboard剪贴板的使用详解(复制、粘贴文字和图片)的更多相关文章
- Swift 中的Closures(闭包)详解
Swift 中的Closures(闭包)详解 在Swift没有发布之前,所有人使用OC语言编写Cocoa上的程序,而其中经常被人们讨论的其中之一 -- Block 一直备受大家的喜爱.在Swift中, ...
- 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件
[源码下载] 重新想象 Windows 8 Store Apps (40) - 剪切板: 复制/粘贴文本, html, 图片, 文件 作者:webabcd 介绍重新想象 Windows 8 Store ...
- vmware12中ubuntu16.10的vmware tools失效,导致不能复制粘贴文字以及自动适应窗口分辨率
问题: 复制命令后,在vmware的ubuntu中粘贴不了,网上说要安装VMWare Tools,但是安装了VMWare Tools 还是不行! 最终找到如下方法: 新安装或异常关机和重新划分分区导致 ...
- 前端复制粘贴文字clipBoard.js的使用
1. vue 中的复制粘贴: <div class="mainTextItem" @click="copyTXTOne" id="copyOn ...
- 【iOS】Swift ?和 !(详解)
Swift语言使用var定义变量,但和别的语言不同,Swift里不会自动给变量赋初始值, 也就是说变量不会有默认值,所以要求使用变量之前必须要对其初始化 .如果在使用变量之前不进行初始化就会报错: [ ...
- [Swift实际操作]八、实用进阶-(9)Swift中的链表LinkedList详解
链表是一种物理存储单元上的非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针连接次序实现的.相比于线性表的顺序结构,链表比较方便插入和删除操作.本文将讲解如何模拟一个链表. //链表的节点 ...
- pandas常用操作详解(复制别人的)——数据透视表操作:pivot_table()
原文链接:https://www.cnblogs.com/Yanjy-OnlyOne/p/11195621.html 一文看懂pandas的透视表pivot_table 一.概述 1.1 什么是透视表 ...
- Ditto —— windows 剪贴板增强小工具(复制粘贴多条记录)
Windows 虽然不断在升级,但系统自带的剪贴板功能却仍然弱爆了 (只能保留一条记录). Ditto 下载地址:http://sourceforge.net/projects/ditto-cp/fi ...
- cocos2d-x 详解之 CCTexture2D(纹理图片)和 CCTextureCache(纹理缓存)
精灵和动画都涉及到纹理图片的使用,所以在研究精灵与动画之前,我们先来了解一下纹理图片类CCTexture2D和纹理缓存CCTextureCache的原理: 当一张图片被加载到内存后,它是以纹理的形式存 ...
随机推荐
- MySQL Replicationation基础
摘要 一.MySQL Replication 介绍MySQL Replication的基本概念,摘自于Mysql官网 二.Replication Configuration 2.1 Basic Ste ...
- 2016NEFU集训第n+3场 G - Tanya and Toys
Description In Berland recently a new collection of toys went on sale. This collection consists of 1 ...
- Vultr优惠码20美元享受20GB SSD和2T流量
美国vps主机商vultr最新优惠码:20FOR30,只限新注册用户,点击注册链接,进入后台Billing,找到Gift code,输入20FOR30,点击apply,就能免费获得20美元的账户余额. ...
- 第三次冲刺spring会议(第五次会议)
[例会时间]2014/5/24 21:15 [例会地点]9#446 [例会形式]轮流发言 [例会主持]马翔 [例会记录]兰梦 小组成员:兰梦 ,马翔,李金吉,赵天,胡佳奇
- 关于No architectures to compile for (ONLY_ACTIVE_ARCH=YES, active arch=x86_64, VALID_ARCHS=armv7 armv7s)使用百度地图的解决办法
出现的原因:armv7s是应用在iPhone 5 A6 的架构上的解决的方式:1,在Project target里“Architectures”设置为“Standard (armv7,armv7s)” ...
- 关于echarts使用的各种问题
此文为作者辛苦编写,如有转发,请注明出处,谢谢 首先引入js文件,这是动态引入 <script src="http://echarts.baidu.com/build/dist/ech ...
- ACPI
高级配置与电源接口(Advanced Configuration and Power Interface),简称ACPI.1997年由Intel.Microsoft.Toshiba 所共同制定提供操作 ...
- Linux openvswitch性能调优
Increasing the flow-eviction threshold The threshold is a type of limit on the number of flows that ...
- mac中Eclipse的快捷键
查看某个类:command + shift +T 快速查看源代码中方法: command + o 选中某个类,command + t:查看此类的父类和子类 如果要导入一个类所在的包名,可以选中这个类, ...
- C语言_IP地址解析
#include<stdio.h> #include<stdlib.h> void main() { unsigned long input_IP; unsigned int ...