是否可以在UIMenuItem
即将弹出时更改UIMenuController
使用的标题?
例如,当我允许copy
中的override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool
菜单,而不是显示标签"复制"时,它会显示标签副本和我要复制的文本喜欢:"复制TEXT_I_M_ABOUT_TO_COPY"?
我想过制作一个自定义UIMenuItem,但是当我创建它时,它必须给出标题的文本。是否可以在它出现之前更改该文本?
答案 0 :(得分:0)
我找到了一种方法,我不知道它是否是最好的,但到目前为止似乎有效。
您必须创建一个UIMenuItem,将其添加到您的UIMenuController共享实例中:
let frame = frameWhereYouWantToMakeTheMenuAppear.frame
let rect = CGRectMake(frame.midX, frame.maxY, 1, 1)
let customPasteMenu = UIMenuItem(title: "Paste", action: "customPaste:")
let menuController = UIMenuController.sharedMenuController()
menuController.menuItems = [customPasteMenu]
menuController.arrowDirection = UIMenuControllerArrowDirection.Up
menuController.setTargetRect(rect, inView: displayView)
menuController.setMenuVisible(true, animated: true)
然后在方法func canPerformAction
中,无论您是否允许操作,都会进行测试,一旦您即将返回true
,就会获得UIMenuController
共享实例,从菜单项列表中获取菜单项并更改标题。
override func canPerformAction(action: Selector, withSender sender: AnyObject?) -> Bool {
if action == "customPaste:" {
// do your tests and get what you want to paste for example
let menuController = UIMenuController.sharedMenuController()
if let customPasteMenu = menuController.menuItems?.first {
customPasteMenu.title = yourTitle + FormatterHelper.formatAmount(stringToPaste)
}
return true
}
return false
}