是否有类似数字键盘的UITextField键盘类型但允许负数?

And*_*dyW 13 objective-c uitextfield ios uikeyboardtype

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

谢谢.

Loc*_*tus 15

这是 Swift 中的一个版本,它有一个 +/- 按钮和一个用于数字键盘的清除和完成按钮。

数字键盘截图

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
    }
}

}
Run Code Online (Sandbox Code Playgroud)


Cla*_*fou 14

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


Oni*_* IV 5

添加减号作为 accesoryView,您需要将 textField 作为属性,在本例中该属性为:myTextField;

\n\n

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

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

并在 viewController 中添加该按钮的方法:

\n\n
-(void)changeNumberSing\n{\nif ([self.myTextField.text hasPrefix:@"-"])\n{\n    self.myTextField.text = [self.myTextField.text substringFromIndex:1];\n}else\n{\n    self.myTextField.text = [NSString stringWithFormat:@"-%@",self.myTextField.text];\n}\n}\n
Run Code Online (Sandbox Code Playgroud)\n