【问题标题】:Is there a UITextField keyboard type that like a number pad but allows negative numbers?是否有类似于数字键盘但允许负数的 UITextField 键盘类型?
【发布时间】:2020-04-02 18:57:29
【问题描述】:

我在我的 UITextFields 上将 UIKeyboardType 设置为 UIKeyboardTypeNumberPad。但是,这只允许用户输入正整数。我需要用户能够输入负整数或正整数。是否有与 UIKeyboardTypeNumberPad 相同但还包含“-”(减号)符号的 UIKeyboardType,如果没有,我可以创建它吗?

谢谢。

【问题讨论】:

  • 创建您的自定义键盘
  • 其他选项,添加一个 (-) 减号按钮作为 inputAccessoryView。或者满意:UIKeyboardTypeNumbersAndPunctuation
  • @OnikIV 如何将减号按钮添加为 inputAccessoryView?
  • @user3210941 代码在答案中

标签: ios objective-c uitextfield uikeyboardtype


【解决方案1】:

这是一个 Swift 版本,它有一个 +/- 按钮,还有一个用于数字键盘的 Clear 和 Done 按钮。

numeric keyboard screenshot

extension UITextField {

func addNumericAccessory(addPlusMinus: Bool) {
    let numberToolbar = UIToolbar()
    numberToolbar.barStyle = UIBarStyle.default

    var accessories : [UIBarButtonItem] = []

    if addPlusMinus {
        accessories.append(UIBarButtonItem(title: "+/-", style: UIBarButtonItem.Style.plain, target: self, action: #selector(plusMinusPressed)))
        accessories.append(UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil))   //add padding after
    }

    accessories.append(UIBarButtonItem(title: "Clear", style: UIBarButtonItem.Style.plain, target: self, action: #selector(numberPadClear)))
    accessories.append(UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: self, action: nil))   //add padding space
    accessories.append(UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.plain, target: self, action: #selector(numberPadDone)))

    numberToolbar.items = accessories
    numberToolbar.sizeToFit()

    inputAccessoryView = numberToolbar
}

@objc func numberPadDone() {
    self.resignFirstResponder()
}

@objc func numberPadClear() {
    self.text = ""
}

@objc func plusMinusPressed() {
    guard let currentText = self.text else {
        return
    }
    if currentText.hasPrefix("-") {
        let offsetIndex = currentText.index(currentText.startIndex, offsetBy: 1)
        let substring = currentText[offsetIndex...]  //remove first character
        self.text = String(substring)
    }
    else {
        self.text = "-" + currentText
    }
}

}

【讨论】:

    【解决方案2】:

    烦人的是,没有这样的系统键盘可用。除了创建自定义键盘之外,您最好的选择是UIKeyboardTypeNumbersAndPunctuation,并进行验证以强制输入有效的数字。

    【讨论】:

    • UIKeyboardTypeDecimalPad 似乎没有减号按钮。为了整洁,我希望它只是所需的键,因此必须考虑创建一个自定义键。
    • 抱歉,我的意思是 UIKeyboardTypeNumbersAndPunctuation,但复制/粘贴了错误的。编辑了答案。
    • 在那种情况下,这实际上是我目前正在做的事情,但是所有的标点符号看起来有点垃圾,你甚至可以切换到这个键盘上的字母表。
    • 我知道,不漂亮!我很想知道苹果的理由是什么。顺便说一句,即使是 UIKeyboardTypeNumbersAndPunctuation 也是有问题的,因为它仅适用于 iPhone,如果你在 iPad 上使用它,你会得到与 UIKeyboardTypeNumbersAndPunctuation 相同的结果。
    • 是吗?到目前为止,我只在 iPhone 上进行过测试,这很糟糕。谢谢。
    【解决方案3】:

    添加一个减号作为accesoryView,你需要将你的textField作为一个属性,在这个例子中这个属性是:myTextField;

    您需要在创建 myTextField 后添加此代码,b.e.在 viewDidLoad 中:

          UIView *inputAccesoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 40)];
    // It´s good idea a view under the button in order to change the color...more custom option
    inputAccesoryView.backgroundColor = [UIColor whiteColor];
    
    UIButton *minusButton = [[UIButton alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width-150)/2, 5, 150, 30)];
    // configure the button here... you choose.
    [minusButton setTitle:@"-" forState:UIControlStateNormal];
    [minusButton addTarget:self action:@selector(changeNumberSing) forControlEvents:UIControlEventTouchUpInside];
    [inputAccesoryView addSubview:minusButton];
    
    self.myTextField.inputAccessoryView = inputAccesoryView;
    

    并在你的viewController中添加这个按钮的方法:

    -(void)changeNumberSing
    {
    if ([self.myTextField.text hasPrefix:@"-"])
    {
        self.myTextField.text = [self.myTextField.text substringFromIndex:1];
    }else
    {
        self.myTextField.text = [NSString stringWithFormat:@"-%@",self.myTextField.text];
    }
    }
    

    【讨论】:

    • 这行得通!只需将按钮标题颜色设置为黑色即可查看文本。这是在键盘上方,是否可以将它放在键盘的左下角(此键盘类型的空白区域)?
    • 没有。您可以在键盘上方绘图。 (例如更改按钮的“y”,按钮将位于键盘上方)。但它不会收到任何事件。
    • 这很烦人。不过感谢您的帮助!
    • @AndyW 可能值得看看“设置为黑色”的东西如何与新的暗模式交互
    • @Shayne 谢谢,但我实际上最终使用了另一个答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 2015-02-05
    • 2012-08-22
    • 2018-06-01
    • 2016-03-02
    • 1970-01-01
    • 2014-06-30
    相关资源
    最近更新 更多