How can I edit the text copied into UIPasteboard

こ雲淡風輕ζ 提交于 2019-12-12 02:52:53

问题


I am trying to manipulate the text the user copied from a UILabel into the UIPasteboard but can't find an example.


回答1:


Here is a full sample view controller for what you are trying to achieve (read the comments to understand what is going on...):

import UIKit
import MobileCoreServices

class ViewController: UIViewController {

  override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)

    // add an observer (self) for whenever the pasteboard contents change
    // this is going to be called whenever the user copies text for example
    NSNotificationCenter
      .defaultCenter()
      .addObserver(
        self,
        selector: "pasteboardChanged:",
        name: UIPasteboardChangedNotification,
        object: nil)


  }

  override func viewWillDisappear(animated: Bool) {
    super.viewWillDisappear(animated)

    // make sure to pair addition/removal of observers
    // we add the observer in viewWillAppear:, so let's 
    // remove it in viewWillDisappear: (here)
    NSNotificationCenter.defaultCenter().removeObserver(self)

  }

  // this will be called whenever the pasteboard changes
  // we specified this function in our observer registration above
  @objc private func pasteboardChanged(notification: NSNotification){

    // this is what the user originally copied into the pasteboard
    let currentPasteboardContents = UIPasteboard.generalPasteboard().string
    // you can now modify whatever was copied
    let newPasteboardContent = " ----- MODIFY THE PASTEBOARD CONTENTS (\(currentPasteboardContents)) AND SET THEM HERE ---------"

    // before we can actually set the new pasteboard contents, we need to make 
    // sure that this method isn't called recursively (we will change the pasteboard's
    // contents, so if we don't remove ourselves from the observer, this method will 
    // be called over and over again, ending up leaving us in an endless loop)
    NSNotificationCenter
      .defaultCenter()
      .removeObserver(
        self,
        name: UIPasteboardChangedNotification,
        object: nil)

    // GREAT! We unregistered ourselves as an observer, now's the time
    // to change the pasteboard contents to whatever we want!
    UIPasteboard.generalPasteboard().string = newPasteboardContent

    // we want to get future changes to the pasteboard, so let's re-add
    // ourselves as an observer
    NSNotificationCenter
      .defaultCenter()
      .addObserver(
        self,
        selector: "pasteboardChanged:",
        name: UIPasteboardChangedNotification,
        object: nil)

  }

}

Make sure you import MobileCoreServices otherwise you won't be able to use some of the code...

Good luck!

EDIT

If you'd like to go a less "hacky" route, I'd suggest you tap into UIMenuController. There is a nice tutorial/guide here:

http://nshipster.com/uimenucontroller/



来源:https://stackoverflow.com/questions/35711080/how-can-i-edit-the-text-copied-into-uipasteboard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!