我们如何使用 UIEditMenuInteraction 与 UITextView 来自定义菜单并添加更多按钮?
在 iOS 16 之前我使用的是:
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
}
}
文档中的
editMenu
解决方案使用标准印刷机,并对突出显示的文本执行功能。
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)
}
}
}