How to Copy Text to Clipboard/Pasteboard with Swift

How to copy text to clipboard/pasteboard with Swift

If all you want is plain text, you can just use the string property. It's both readable and writable:

// write to clipboard
UIPasteboard.general.string = "Hello world"

// read from clipboard
let content = UIPasteboard.general.string

(When reading from the clipboard, the UIPasteboard documentation also suggests you might want to first check hasStrings, "to avoid causing the system to needlessly attempt to fetch data before it is needed or when the data might not be present", such as when using Handoff.)

SwiftUI - how to copy text to clipboard?

Use the following - put shown text into pasteboard for specific type (and you can set as many values and types as needed)

Update: for Xcode 13+, because of "'kUTTypePlainText' was deprecated in iOS 15.0..." warning

import UniformTypeIdentifiers

Text(self.BLEinfo.sendRcvLog)
.onTapGesture(count: 2) {
UIPasteboard.general.setValue(self.BLEinfo.sendRcvLog,
forPasteboardType: UTType.plainText.identifier)
}

for older versions:

import MobileCoreServices // << for UTI types

// ... other code

Text(self.BLEinfo.sendRcvLog)
.onTapGesture(count: 2) {
UIPasteboard.general.setValue(self.BLEinfo.sendRcvLog,
forPasteboardType: kUTTypePlainText as String)
}

Swift 3 copy string to clipboard

Swift 3 you copy it like this way.

 let pasteboard = NSPasteboard.general()
pasteboard.declareTypes([NSPasteboardTypeString], owner: nil)
pasteboard.setString("Good Morning", forType: NSPasteboardTypeString)

MacOS swiftUI copy the text from the clipboard into a TextField field

try this approach, to show what you read from the Pasteboard into the TextField text: (use cmd+c to copy some text into the pasteboard before you run the app)

struct ContentView: View {
@State var repoUrlStr = ""

var body: some View {
TextField("Git Repository URL", text: $repoUrlStr)
.lineLimit(1)
.padding(.bottom, 15)
.frame(width: 300)
.onAppear {
if let read = NSPasteboard.general.string(forType: .string) {
repoUrlStr = read // <-- here
}
}.frame(width: 444, height: 444)
}
}

Placing *both* string and image on pasteboard

You should use kUTTypeUTF8PlainText as type key.

BR
Dirk

How do I paste programmatically with Swift?

You should just be able to convert those to Swift:

@IBAction func copy() {
let pb: UIPasteboard = UIPasteboard.generalPasteboard();
pb.string = textView.text // Or another source of text
}

@IBAction func paste() {
let pb: UIPasteboard = UIPasteboard.generalPasteboard();
textView.text /*(Or somewhere to put the text)*/ = pb.string
}

Swift button press copy text

do like

Copy

 func buttonViewLinkAction(sender:UIButton!) {
print("Button tapped")
UIPasteboard.generalPasteboard().string = yourstring!.text() // or use sender.titleLabel.text

}

paste or Retrieve

func GetCopiedText(sender: UIButton!) {

if let myString = UIPasteboard.generalPasteboard().string {
print(myString)
}

}

Update

 func buttonViewLinkAction(sender:UIButton!) {
print(sender.currentTitle)
print(sender.titleLabel.text)
}

update-2

you written the buttonViewLinkAction code in inside the func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) its never call. so remove the buttonViewLinkAction and add outside the method of data source

 //fontawesome link
cell.buttonViewLink.setTitle(String.fontAwesomeIconWithName(.Link), forState: .Normal)
cell.buttonViewLink.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)

func buttonViewLinkAction(sender:UIButton!) {
print("Button tapped")
UIPasteboard.generalPasteboard().string = "Label text"

}

Final Answer

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CellClass
let face = self.faces[indexPath.item]

//set image and align center
if let imageURL = NSURL(string:face.image) {
cell.imageView.sd_setImageWithURL(imageURL)

} else {
cell.imageView.image = self.placeholderImage
}

//set name
if let imageNAME: String = String(face.name){
cell.labelView.text = (imageNAME .uppercaseString)

} else {
cell.labelView.text = "oops name"
}

//set border
cell.layer.shadowOffset = CGSizeMake(0, 1)
cell.layer.shadowColor = UIColor.blackColor().CGColor
cell.layer.shadowRadius = 1
cell.layer.shadowOpacity = 0.1
cell.clipsToBounds = false
let shadowFrame: CGRect = (cell.layer.bounds)
let shadowPath: CGPathRef = UIBezierPath(rect: shadowFrame).CGPath
cell.layer.shadowPath = shadowPath

//square background button
cell.buttonViewSquare.backgroundColor = UIColor(red: 249/255, green: 249/255, blue: 249/255, alpha: 1)
cell.buttonViewSquare.enabled = false

//fontawesome link
cell.buttonViewLink.setTitle(String.fontAwesomeIconWithName(.Link), forState: .Normal)
cell.buttonViewLink.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
cell.buttonViewLink.addTarget(self, action: "buttonViewLink:", forControlEvents: UIControlEvents.TouchUpInside)
// or use like cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)

//fontawesome heart
cell.buttonViewHeart.setTitle(String.fontAwesomeIconWithName(.Heart), forState: .Normal)
cell.buttonViewHeart.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
cell.buttonViewHeart.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)

//fontawesome share
cell.buttonViewShare.setTitle(String.fontAwesomeIconWithName(.Share), forState: .Normal)
cell.buttonViewShare.titleLabel?.font = UIFont.fontAwesomeOfSize(20)

return cell
}

call method like

  func buttonViewLink(sender:UIButton!) {
print("Button tapped")
UIPasteboard.generalPasteboard().string = sender.titleLabel.text

}

or use directly already you have a function

  @IBAction func buttonViewLinkAction(sender: UIButton) {
print("Button tapped")
UIPasteboard.generalPasteboard().string = sender.titleLabel.text
}

modified Answer

add tag in here cell.buttonViewLink.tag = indexPath.item

     cell.buttonViewLink.setTitle(String.fontAwesomeIconWithName(.Link), forState: .Normal)
cell.buttonViewLink.titleLabel?.font = UIFont.fontAwesomeOfSize(20)
cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)
cell.buttonViewLink.tag = indexPath.item

and call the methof like

@IBAction func buttonViewLinkAction(sender: UIButton) {
print("Button tapped")
let face = self.faces[sender.tag]
if let imageNAME: String = String(face.name){
print(imageNAME .uppercaseString)
}
if let imageURL = NSURL(string:face.image) {
print(imageURL)

}
UIPasteboard.generalPasteboard().string = sender.titleLabel.text
}

Copy UITextView to PasteBoard

Well, the problem is that you are using setString: to store UITextView, which is an UIKit control and not NSString, in pasteboard. What you probably mean is to store its text value.

Objective-C does not support implicit conversions like Scala or Swift. Solution is simple, just access the text property explicitly:

[appPasteBoard setString:_DescriptionLabel.text];

I encourage you to look into UIPasteboard documentation for details concerning it API: https://developer.apple.com/library/prerelease/ios/documentation/UIKit/Reference/UIPasteboard_Class/index.html



Related Topics



Leave a reply



Submit