简体   繁体   English

显示以编程方式创建的 UITextView/UITextField 的剪切/复制/粘贴菜单

[英]Display cut / copy / paste menu for UITextView / UITextField created programmatically

I cannot seem to get the default behaviour when creating a UITextView or a UITextField programmatically compared to coming from the Storyboard.与来自 Storyboard 相比,以编程方式创建 UITextView 或 UITextField 时,我似乎无法获得默认行为。

I instantiate the text view in the view controller as a property like this:我将视图控制器中的文本视图实例化为这样的属性:

lazy var descriptionTextView: UITextView = {
       let tv = UITextView(frame: .zero)
        tv.font = UIFont.systemFont(ofSize: UIFont.systemFontSize)
        tv.clipsToBounds = true
        tv.layer.cornerRadius = 5.0
        tv.layer.borderWidth = 0.25
        tv.translatesAutoresizingMaskIntoConstraints = false
        return tv
    }()

In loadView I add it to the view and configure auto layout.loadView我将其添加到视图并配置自动布局。 It displays correctly, so that's not the issue.它显示正确,所以这不是问题。

My problem is, that when selecting text within that text view, I expect the following menu being displayed (screenshot taken from a storyboard project):我的问题是,在该文本视图中选择文本时,我希望显示以下菜单(从故事板项目中截取的屏幕截图):

在此处输入图片说明

What I do see though is just the menu bar showing correction proposals if single words are selected or even nothing at all when several words are selected.我所看到的只是菜单栏显示更正建议,如果选择了单个单词,或者在选择多个单词时甚至什么都不显示。 How can I obtain the same behaviour in my programmatic approach?如何在我的程序化方法中获得相同的行为?

Edit:编辑:

Here's how I add the textview to the view (simplified, there are other views that I omitted here):下面是我将 textview 添加到视图的方法(简化了,我在这里省略了其他视图):

override func loadView() {
        view = UIView()
        view.backgroundColor = .white

        view.addSubview(descriptionTextView)

        NSLayoutConstraint.activate([            
            descriptionTextView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor, constant: 12),
            descriptionTextView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: 24),
            descriptionTextView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -24),
            descriptionTextView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: -12)
        ])
}

When I select a single word within the textfield, I only get correction suggestions:当我在文本字段中选择一个单词时,我只会得到更正建议:

在此处输入图片说明

When I select more than one word, the menu is not popping up at all:当我选择多个单词时,菜单根本没有弹出:

在此处输入图片说明

Try this example:试试这个例子:

@objc func longPressEventHandler(sender: UILongPressGestureRecognizer) {
    guard sender.state == .began,
        let senderView = sender.view,
        let superView = sender.view?.superview
        else { return }

    // Make responsiveView the window's first responder
    senderView.becomeFirstResponder()

    // Set up the shared UIMenuController
    let cutMenuItem = UIMenuItem(title: "Cut", action: #selector(cutTapped))
    let copyMenuItem = UIMenuItem(title: "Copy", action: #selector(copyTapped))
    UIMenuController.shared.menuItems = [cutMenuItem, copyMenuItem]

    // Tell the menu controller the first responder's frame and its super view
    UIMenuController.shared.setTargetRect(senderView.frame, in: superView)

    // Animate the menu onto view
    UIMenuController.shared.setMenuVisible(true, animated: true)
}

Turns out that I introduced the problem by implementing scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) incorrectly:原来我是通过错误地实现scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)引入问题的:

actually I called makeKeyAndVisible() before setting window?.windowScene = windowScene .实际上我设置window?.windowScene = windowScene之前调用了makeKeyAndVisible()

Seems like the standard cut / copy / paste menu is contained in that property...似乎标准剪切/复制/粘贴菜单包含在该属性中...

So, to clarify, here's willConnectTo with the mistake...:因此,要澄清一下,这里的willConnectTo错误...:

guard let windowScene = (scene as? UIWindowScene) else { return }

window = UIWindow(frame: UIScreen.main.bounds)

let mainViewController = ProjectTableViewController()

let navigationController = UINavigationController()
navigationController.setViewControllers([mainViewController], animated: false)

window?.rootViewController = navigationController
window?.makeKeyAndVisible()
window?.windowScene = windowScene //that's too late... :-(

...and here's the working code: ...这是工作代码:

guard let windowScene = (scene as? UIWindowScene) else { return }

window = UIWindow(frame: UIScreen.main.bounds)

let mainViewController = ProjectTableViewController()

let navigationController = UINavigationController()
navigationController.setViewControllers([mainViewController], animated: false)

window?.rootViewController = navigationController
window?.windowScene = windowScene //that's better :-)
window?.makeKeyAndVisible()

I know: what a stupid mistake to make – I hope my answer can help anyone else who runs into this...我知道:犯了多么愚蠢的错误-我希望我的回答可以帮助遇到此问题的任何其他人...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用IBInspectable禁用UITextField上的剪切,复制和粘贴 - Disable cut, copy, paste on UITextField using IBInspectable 缺少 UITextField/UIWebView 中的复制/粘贴菜单 - missing copy/paste menu in UITextField/UIWebView 当我以编程方式在UITextField中选择文本时,如何不显示全选/全选/剪切/复制/粘贴栏 - How can I keep the select / select all / cut / copy / paste bar from appearing when I programmatically select text in UITextField 在 UILabel 上显示 iPhone 剪切复制粘贴菜单 - Show iPhone cut copy paste menu on UILabel 在iOS上本地化剪切|复制|粘贴菜单 - Localizing the Cut|Copy|Paste menu on iOS 将UITextField字符串复制到UITextView - Copy UITextField string to UITextView 如何以编程方式从 UITextField 禁用复制粘贴选项 - How to disable copy paste option from UITextField programmatically 如何按方向更改在以编程方式创建的滚动视图中调整以编程方式创建的UILabel,UITextView,UITextfield的大小 - How to resize the programmatically created UILabel,UITextView,UITextfield in programmatically created scroll view as per orientation change 取消UITextView中的选择/粘贴菜单? - Dismiss select/paste menu in UITextView? UIWebView execCommand剪切,复制,粘贴 - UIWebView execCommand Cut, Copy, Paste
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM