Display cut / copy / paste menu for UITextView / UITextField created programmatically
Try this example:
@objc func longPressEventHandler(sender: UILongPressGestureRecognizer) {
guard sender.state == .began,
let senderView = sender.view,
let superView = sender.view?.superview
else { return }
// Make responsiveView the window's first responder
senderView.becomeFirstResponder()
// Set up the shared UIMenuController
let cutMenuItem = UIMenuItem(title: "Cut", action: #selector(cutTapped))
let copyMenuItem = UIMenuItem(title: "Copy", action: #selector(copyTapped))
UIMenuController.shared.menuItems = [cutMenuItem, copyMenuItem]
// Tell the menu controller the first responder's frame and its super view
UIMenuController.shared.setTargetRect(senderView.frame, in: superView)
// Animate the menu onto view
UIMenuController.shared.setMenuVisible(true, animated: true)
}
Allow user to select text from UILabel to copy
It is not possible with UILabel
.
You should use UITextView
for that. Just disable editing using textFieldShouldBeginEditing
delegate method.
Can I implement select and copy on UILabel when it has a long press just as UITextField (UITextView、UIWebView), and how?
You're gonna have to implement it yourself. There isn't anything too special about any of the system text views, just read the docs for UIPasteboard
, specifically around -[UIPasteboard setString:]
. The rest just comes down to how you want to implement the UI. For that I'd recommend looking into subclassing UILabel
just to keep everything tidy; UIMenuController
for showing the callout view; implementing -canBecomeFirstResponder
, -canPerformAction:forSender:
, and -copy:
to customize the callout actions; and UILongPressGestureRecognizer
for triggering everything.
iOS create copy paste like popover (UIMenuController) in UITableView
If it helps anyone, the keyword to look for is "UIMenuController"
I finally managed to solve this by implementing a custom UITableViewCell
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
let showPasswordItem = UIMenuItem(title: "Show Password", action: #selector(showPass(_:)))
let copyUserNameItem = UIMenuItem(title: "Copy Username", action: #selector(copyUsername(_:)))
let copyPasswordItem = UIMenuItem(title: "Copy Password", action: #selector(copyPass(_:)))
self.isPasswordShowing = !self.password.isSecureTextEntry
UIMenuController.shared.menuItems?.removeAll()
UIMenuController.shared.menuItems = [copyPasswordItem,copyUserNameItem,showPasswordItem,hidePasswordItem]
UIMenuController.shared.update()
if selected {
self.becomeFirstResponder()
let menu = UIMenuController.shared
menu.setTargetRect(self.contentView.frame, in: self.contentView.superview!)
menu.setMenuVisible(true, animated: true)
}
}
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
return action == #selector(showPass(_:)) || action == #selector(copyUsername(_:)) || action == #selector(copyPass(_:))
}
override var canBecomeFirstResponder : Bool {
return true
}
func showPass(_ send:AnyObject){
self.password.isSecureTextEntry = false
self.isPasswordShowing = true
}
func copyUsername(_ send:AnyObject){
UIPasteboard.general.string = self.username.text
}
func copyPass(_ send:AnyObject){
UIPasteboard.general.string = self.password.text
}
func hidePass(_ send:AnyObject){
self.password.isSecureTextEntry = true
self.isPasswordShowing = false
}
iphone copy/paste menu not shown in UITableViewCell
Somehow the copy/paste menu starts showing up when I tap the textfield or textview. Not clear on what I have done that made the difference. But I can tell you what I did for your reference. I tried to married NIB file, Custome UIViewController, UITableViewCell into one. I had the textfield and textview embeded in a UIViewController. The UIViewController userinterface is loaded from a NIB file. I then have the UIViewController view added as a subview into the tableViewCell. You can do some googling on this. There are many suggestions.
I do agree with some people who advice not to load NIB into an UITableViewCell. In the end, I took the advice and coded the user interface entirely without the NIB file. I declared a subclass of UITableViewCell then had thd user interface constructed within the initWithStyle. This way the custom tableViewCell was readily useful in the tableView without the headache of figuring out how to load the NIB file and how to make the UIViewController reuseable as a tableViewCell.
Since the code was a lot cleaner, I probably accidentally removed the bad code that had broke the copy/paste mechanism.
Copying a text from UILabel in iPhone
This github project is compiled fine and I have tested it it's working
UILabel copy using menu
Related Topics
Ask for User Permission to Receive Uilocalnotifications in iOS 8
When Do I Need to Call Setneedsdisplay in iOS
How to Dismiss a Storyboard Popover
Disable Swipe Back Gesture in Swift
Barcode Generation from Within iOS App
Broken Uisearchbar Animation Embedded in Navigationitem
How to Add a Image in Email Body Using Mfmailcomposeviewcontroller
Facebook Login - iOS 9 - Without Safari
Facebook Sdk 3.1 for iOS - Runs on iOS6, But Crashes on iOS 5.X
Is It Possible For Uistackview to Scroll
How to Set Hex Color Code for Background
What #Defines Are Set Up by Xcode When Compiling for Iphone
Get Current iPhone Device Timezone Date and Time from Utc-5 Timezone Date and Time iPhone App
Find an Item and Change Value in Custom Object Array - Swift
Nsinteger and Nsuinteger in a Mixed 64Bit/32Bit Environment
Display Done Button on Uipickerview
Scroll to Bottom of Uitextview Erratic in iOS 7
Proper Use of Loadview and Viewdidload with Uiviewcontroller Without Nibs/Xibs