【问题标题】:UItextField restrict user to enter number only IOS app on ipadUItextField 限制用户在 ipad 上仅输入数字 IOS 应用程序
【发布时间】:2016-09-22 14:46:24
【问题描述】:

我正在为 Ipad 开发一个应用程序。我正在设计一个忘记密码屏幕以允许用户输入密码UITextField。按照设计,密码只允许输入数字。我可以在 Iphone 中将 UITextFiled keyboardtype 设置为 phonepad 但该选项似乎不适用于 Ipad(Ipad 总是显示完整的键盘布局)。我们如何为只有数字的 Ipad 应用程序实现键盘?

我必须设计自己的键盘布局吗? 非常感谢任何帮助。谢谢!

【问题讨论】:

    标签: ios ipad keyboard


    【解决方案1】:

    键盘类型并不规定文本字段接受哪种输入,即使您使用仅显示数字的自定义键盘,用户也可以随时粘贴某些内容或使用外部硬件键盘。

    要做也就是说,您需要观察输入,例如,通过成为 UITextFieldDelegate 然后:
    swift中的示例:

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool{
        // non decimal digit character set, better save this as a property instead of creating it for each keyboard stroke
        let non_digits = NSCharacterSet.decimalDigits.inverted
        // Find location for non digits
        let range = string.rangeOfCharacter(from: non_digits)
        if range == nil { // no non digits found, allow change
            return true
        }
        return false // range was valid, meaning non digits were found
    }
    

    这将防止任何非数字字符被添加到文本字段中。

    【讨论】:

    • 感谢您的帮助。对于 ipad 应用程序中的键盘布局。我们可以更改默认布局(所有字符、数字符号的布局...)吗?
    • @LêKhánhVinh iPad 没有像 iPhone 那样的十进制布局,我看到许多使用 PIN 码的应用程序为此绘制了自己的键盘。
    • 您知道任何库或示例,以便我们可以遵循以实现 ipad 应用程序的数字键盘吗?
    • @LêKhánhVinh 请参阅this question 了解一些选项,从未尝试过。
    【解决方案2】:

    iPad 上没有内置的纯数字(电话/PIN)键盘 如果你想在 iPad 上实现这个,你需要实现自己的键盘。

    有很多例子:

    https://github.com/azu/NumericKeypad

    https://github.com/lnafziger/Numberpad

    https://github.com/benzado/HSNumericField

    【讨论】:

      【解决方案3】:

      是的,我在 iPad 上也面临同样的问题,因此我使用了这个:

      func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
          // to avoid any other characters except digits
          return string.rangeOfCharacter(from: CharacterSet(charactersIn:"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-=_+`~[{]}|\\: ;\"/?>.<,'")) == nil
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多