我需要在文本视图中长按以将“突出显示”添加为新的自定义菜单项。 我已经能够删除不需要的“查找”,但无法弄清楚如何添加额外的菜单项。
class CustomUITextView: UITextView {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(UIResponderStandardEditActions.cut(_:)) ||
action == Selector(("_define:")) ||
action == #selector(UIResponderStandardEditActions.paste(_:)) ||
action == Selector(("_promptForReplace:")){
return false;
}
else {
return super.canPerformAction(action, withSender: sender)
}
}
}
struct TextView : UIViewRepresentable {
var contents: NSAttributedString
func makeUIView(context: Context) -> CustomUITextView {
let customTextView = CustomUITextView()
customTextView.isEditable = false
customTextView.isUserInteractionEnabled = true
return customTextView
}
func updateUIView(_ uiView: CustomUITextView, context: Context) {
uiView.attributedText = contents
}
}
func addSpecialMenu() {
let menuController = UIMenuController.shared
var menuItem = [UIMenuItem]();
let highlightMenuItem = UIMenuItem(title: "Highlight", action: #selector(onHighlightClicked));
menuItem.append(highlightMenuItem);
menuController.menuItems = menuItem;
}
我尝试在longPressGesture上调用addSpecialMenu()。但是无法正常工作。 如何在SwiftUI中添加自定义菜单链接??