Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift button press copy text

Tags:

xcode

ios

swift

I have a button inside a cell inside a collection view, everything works fine except for the copy function I'm trying to create. When I click on the button the text is not copied or in my test case the text is not printed to console.

    cell.buttonViewLink.addTarget(self, action: "buttonViewLinkAction:", forControlEvents: UIControlEvents.TouchUpInside)

    func buttonViewLinkAction(sender:UIButton!) {
        print("Button tapped")

    }

In my storyboard I have a touch up inside action linked from the button to the view (I created this with ctrl-drag form the button to view). Everything looks OK but yet when I press the button nothing happens, doesn't crash either as things look OK.

What am I missing?

like image 499
user3591436 Avatar asked Dec 25 '22 02:12

user3591436


2 Answers

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
  }
like image 86
Anbu.Karthik Avatar answered Jan 07 '23 22:01

Anbu.Karthik


Update Swift 4.2

override func viewDidLoad() {
    super.viewDidLoad()
    label.addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(copyValuePressed)))
}

@objc func copyValuePressed() {
    UIPasteboard.general.string = label.text
}
like image 44
sazz Avatar answered Jan 07 '23 21:01

sazz