简体   繁体   中英

catch the copy/ paste event UIMenuController

Is there any way to catch the copy/paste event in UIMenuController? I want to set flags when user tap copy option in menu on UIMenuController.

Thanks in advance!

In order to catch the copy/paste event you have to subclass each UI component where you'd like to catch it.

Simple example with UILabel is shown in Make UILabel Copyable in Swift post. If you use their final code, don't forget to assign SRCopyableLabel as a base class for UILabel in storyboard. Also, modify the required init method in SRCopyableLabel class like that:

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    sharedInit()
}

Then, whenever a user taps on "Copy", for instance, the corresponding "copy" method from SRCopyableLabel is called and you can do whatever you want in there:

override func copy(sender: AnyObject?) {
    let board = UIPasteboard.generalPasteboard()
    board.string = text
    let menu = UIMenuController.sharedMenuController()
    menu.setMenuVisible(false, animated: true)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM