Reputation: 3955
I need to add a custom action to the edit menu that pops up when a user selects some text in a UITextView in iOS.
How do I do this?
Upvotes: 14
Views: 12790
Reputation: 366
since UIMenuItem was deprecated, either you must
editMenuInteraction
solutioneditMenu
functionUsing editMenu
function is straight forward. I've also posted my solution in: stackoverflow 73712955
private class CustomUITextView: UITextView, UIEditMenuInteractionDelegate {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
// A little short-circuit for our new function to run.
if action == #selector(printToConsole) {
return true
}
return super.canPerformAction(action, withSender: sender)
}
override func editMenu(for textRange: UITextRange, suggestedActions: [UIMenuElement]) -> UIMenu? {
let refreshAction = UIAction(title: "Print to Console") { (action) in
self.printToConsole()
}
var actions = suggestedActions
actions.insert(refreshAction, at: 0) // insert at the front of the menu.
return UIMenu(children: actions)
}
@objc func printToConsole() {
if let range = self.selectedTextRange, let selectedText = self.text(in: range) {
print(selectedText)
}
}
}
Looks like this
and it works printing to console.
Upvotes: 2
Reputation: 99
Here is how you create a custom edit menu in swift 5:
import UIKit
class ViewController: UIViewController {
@IBOutlet var textfield: UIView!
override func viewDidLoad() {
super.viewDidLoad()
let changeBackground = UIMenuItem(title: "Change Background Colour", action: #selector(changeBackgroundColour))
UIMenuController.shared.menuItems = [changeBackground] //will add it to everything that has
}
@objc func changeBackgroundColour()
{
self.view.backgroundColor = .cyan //just makes the background colour cyan
}
}
I have also made a youtube video explaining this here
Upvotes: 1
Reputation: 540
SWIFT 5
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
addCustomMenu()
}
func addCustomMenu() {
//Xcode doesn't like printToConsole being a var and a function call
let printToConsole = UIMenuItem(title: "Print To Console", action: #selector(printToConsole2))
UIMenuController.shared.menuItems = [printToConsole]
}
@objc func printToConsole2() {
if let range = textView.selectedTextRange, let selectedText = textView.text(in: range) {
print(selectedText)
}
}
}
Upvotes: 4
Reputation: 1595
class ViewController: UIViewController, UITextViewDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
addCustomMenu()
}
func addCustomMenu() {
let printToConsole = UIMenuItem(title: "Print To Console", action: #selector(printToConsole))
UIMenuController.shared().menuItems = [printToConsole]
}
func printToConsole() {
if let range = textView.selectedTextRange, let selectedText = textView.text(in: range) {
print(selectedText)
}
}
}
This is an example of text selection menu item that changes the text in a UITextView
to red. changeToRedFunc
can perform any action you want.
Note: This is in Swift 3 (ask if you want it in Swift 2.3)
Hope this helps! If you have any questions feel free to ask! :D
Upvotes: 23