Uitextfield, Uitextview component Systems natively support text replication, but sometimes we need other components to replicate, such as clicking on the text on the copy uilabel, images in Uiimageview, UITableView the contents of the cell, or click the button to automatically copy text or pictures to the pasteboard, and so on.
These we can achieve with the aid of Uipasteboard.
One, write the content to the Clipboard
1, copy string
Uipasteboard.generalpasteboard (). String = "Welcome to hangge.com"
2, copy string array
Uipasteboard.generalpasteboard () strings = ["Hellow", "hangge.com"]
3, copy the picture
Let image = UIImage (named: "Logo.png")
Uipasteboard.generalpasteboard (). Image = Image
4, copy binary data (NSData)
Let path = Nsbundle.mainbundle (). Pathforresource ("logo", OfType: "PNG")!
Let Filedata = NSData (Contentsoffile:path)!
Uipasteboard.generalpasteboard (). SetData (Filedata, Forpasteboardtype: "Public.png")
Note: Get Binary data from clipboard (NSData)
Let MyData = Uipasteboard.generalpasteboard (). Dataforpasteboardtype ("Public.png")
Second, common components to increase replication capabilities
1, let the text label (Uilabel) support the copy function
We customize a replicable tag class Uicopylabel (inherited Uilabel), which can respond to touch events and display the Copy menu
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)
}
}
Copy
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
}
}
You can copy the contents of the text label after it has been pressed long:
2, let the Picture control (Uiimageview) support copy, paste function
We customize a picture control class Uicpimageview (inherited Uiimageview), and also add a touch event response internally. This control not only supports replication, but also supports pasting.
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)
}
}
Copy
Override func copy (Sender:anyobject?) {
Let board = Uipasteboard.generalpasteboard ()
Board.image = Self.image
Let menu = Uimenucontroller.sharedmenucontroller ()
Menu.setmenuvisible (False, Animated:true)
}
Paste
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
}
}
Below we add two Uicpimageview to the interface, we can copy the picture in the left control to the right control, the effect chart is as follows:
3, let the table (UITableView) Support the copy function
Import Uikit
Class Viewcontroller:uiviewcontroller, Uitableviewdelegate, Uitableviewdatasource {
var tableview:uitableview?
var tabledata = ["Entry 1", "Entry 2", "Entry 3", "Entry 4", "Entry 5", "Entry 6", "Entry 7"]
Override Func Loadview () {
Super.loadview ()
}
Override Func Viewdidload () {
Super.viewdidload ()
Create a table view
Self.tableview = UITableView (Frame:self.view.frame, style:. Plain)
self.tableview!. delegate = Self
self.tableview!. DataSource = Self
To create a reused cell
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
}
In this case, there is only one partition
Func Numberofsectionsintableview (Tableview:uitableview)-> Int {
return 1;
}
Returns the number of table rows (that is, returns the number of controls)
Func TableView (Tableview:uitableview, numberofrowsinsection section:int)-> Int {
Return Tabledata.count
}
Create each cell display (create a parameter indexpath a specified cell)
Func TableView (Tableview:uitableview, Cellforrowatindexpath Indexpath:nsindexpath)
-> UITableViewCell
{
In order to provide tabular display performance, the completed cells need to be reused
Let identify:string = "Swiftcell"
Cells in the same form are reused and registered at the time of declaration
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 ()
}
}
You can copy the cell content by pressing a cell long: