如何仅使用一次性代码自动填充支持从用户中禁用输入文字

发布于 2025-01-20 15:30:54 字数 187 浏览 3 评论 0原文

我想禁用从一次性代码文本字段手动输入文本,而用户只能从键盘快速输入栏点击 SMS OTP 代码。

我从 Whatsapp 中得到的另一个问题是,他们的输入会自动显示键盘 Quicktype 栏,而我的则不会,除非我调用 becomeFirstResponder

我怎样才能实现这一点?

谢谢。

I want to disable manual text entering from one-time-code textField while the user only can tap SMS OTP Code from Keyboard Quicktype Bar.

Another question i got from seeing whatsapp is that their input shows the Keyboard Quicktype bar automatically while mine is not unless i call becomeFirstResponder

How can i achieve this?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮年 2025-01-27 15:30:54

您可以尝试此操作,也许它适用于您的用例

删除textfield.iseNabled = false如果在

添加textfield.delegate = self之前添加了它,以便我们可以管理时用户添加输入

添加textfield.becomefirstresponder()以使键盘出现

,然后实现此uitextfielddelegate回调

extension YourViewOrViewController: UITextFieldDelegate {
    
    func textField(_ textField: UITextField,
                   shouldChangeCharactersIn range: NSRange,
                   replacementString string: String) -> Bool {
        
        // Only allow multiple characters to be set like the OTP
        // Or define your own logic when you want text to be
        // accepted into the text field
        return string.count != 1
    }
}

检查是否为您提供所需的结果

You can try this, maybe it works for your use case

Remove textField.isEnabled = false if you added it before

Add textField.delegate = self so we can manage what happens when user adds input

Add textField.becomeFirstResponder() to make the keyboard appear

Then implement this UITextFieldDelegate callback

extension YourViewOrViewController: UITextFieldDelegate {
    
    func textField(_ textField: UITextField,
                   shouldChangeCharactersIn range: NSRange,
                   replacementString string: String) -> Bool {
        
        // Only allow multiple characters to be set like the OTP
        // Or define your own logic when you want text to be
        // accepted into the text field
        return string.count != 1
    }
}

Check if this gives you the desired result

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文