UILabel selectable content

I have a list of URLs in an iOS Label object. It does not seem that the user can select and copy the item from the list and paste it into their Safari browser on the iOS device. Is there any way to do this?

+4
source share
2 answers

This feature is not available in UILabel .

You need to use UITextField or UITextView . Also do not forget to change its appearance and use

 [... setEditable:NO]; 
+5
source

This is actually possible with UILabel, only you will need to do some subclasses.

The end result: when your user clicks on the label for a long time, he or she will see a copy of the ball.

Image showing a copy of the ball after a long tap on iOS

Below are the steps to make a copy with a copy (as I recall):

  • subclass of UILabel
  • set userInteractionEnabled = YES
  • override canBecomeFirstResponder and return true
  • add UILongPressGestureRecognizer
  • will be the first responder and representative of UIMenuController

Swift 3:

 let menu = UIMenuController.shared if !menu.isMenuVisible { self.becomeFirstResponder() menu.setTargetRect(self.bounds, in: self) menu.setMenuVisible(true, animated: true) } 
  1. override canPerformAction to allow copying

Swift 3:

 override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool { return action == #selector(UIResponderStandardEditActions.copy(_:)) } 
  1. override copy method, UIP Download text and hide UIMenuController

Swift 3:

  let menu = UIMenuController.shared let labelText = self.text ?? self.attributedText?.string if let uLabelText = labelText { let clipBoard = UIPasteboard.general clipBoard.string = uText } menu.setMenuVisible(false, animated: true) 
+5
source

All Articles