How to insert an option within the menu that appears when text is selected in SwiftUI?

632 views Asked by At

I am developing an application with SwiftUI where i would like to insert an option in the menu that appears when text is selected, it was a custom item for this menu. menu

I want to know if it was possible to make this feature. thanks

1

There are 1 answers

1
dibs On

You need to use canPerformAction

for example if you only want to keep "copy", "Select all" and add the "comment" button

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            let menu = UIMenuController.shared
            let newInstanceItem = UIMenuItem(title: "Comment", action:#selector(reportAFault))
            menu.menuItems = [newInstanceItem]
            menu.update()
            if action == #selector(copy(_:)) || action == #selector(selectAll(_:)) || action == #selector(commentThisText){
                
                return true
            }
            return false            
    }
    

    
    @objc func commentThisText() {
            YOUR CODE
    }