How can we use UIEditMenuInteraction with UITextView to customize menu and add more buttons?
Before iOS 16 I was using:
UIMenuController.shared.menuItems = [menuItem1, menuItem2, menuItem3]
How can we use UIEditMenuInteraction with UITextView to customize menu and add more buttons?
Before iOS 16 I was using:
UIMenuController.shared.menuItems = [menuItem1, menuItem2, menuItem3]
class ViewController: UIViewController {
@IBOutlet weak var txtView: UITextView!
var editMenuInteraction: UIEditMenuInteraction?
override func viewDidLoad() {
super.viewDidLoad()
setupEditMenuInteraction()
}
private func setupEditMenuInteraction() {
// Addding Menu Interaction to TextView
editMenuInteraction = UIEditMenuInteraction(delegate: self)
txtView.addInteraction(editMenuInteraction!)
// Addding Long Press Gesture
let longPressGestureRecognizer =
UILongPressGestureRecognizer(target: self, action: #selector(handleLongPress(_:)))
txtView.addGestureRecognizer(longPressGestureRecognizer)
}
@objc
func handleLongPress(_ gestureRecognizer: UILongPressGestureRecognizer) {
guard gestureRecognizer.state == .began else { return }
let configuration = UIEditMenuConfiguration(
identifier: "textViewEdit",
sourcePoint: gestureRecognizer.location(in: txtView)
)
editMenuInteraction?.presentEditMenu(with: configuration)
}
}
extension ViewController: UIEditMenuInteractionDelegate {
func editMenuInteraction(_ interaction: UIEditMenuInteraction,
menuFor configuration: UIEditMenuConfiguration,
suggestedActions: [UIMenuElement]) -> UIMenu? {
var actions = suggestedActions
let customMenu = UIMenu(title: "", options: .displayInline, children: [
UIAction(title: "menuItem1") { _ in
print("menuItem1")
},
UIAction(title: "menuItem2") { _ in
print("menuItem2")
},
UIAction(title: "menuItem3") { _ in
print("menuItem3")
}
])
actions.append(customMenu)
return UIMenu(children: actions) // For Custom and Suggested Menu
return UIMenu(children: customMenu.children) // For Custom Menu Only
}
}
func editMenu(for textRange: UITextRange, suggestedActions: [UIMenuElement]) -> UIMenu?
developer.apple.com/documentation/uikit/uitextinput/… –
Septum UITextInput
protocol method, I haven't checked that yet. –
Aureolin The editMenu
solution from the documentation that uses the standard press, and executes a function on highlighted text.
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)
}
}
}
© 2022 - 2025 — McMap. All rights reserved.