9

如何在数字键盘键盘中添加点?当我使用通知 UIKeyboardDidShowNotification 时,我希望 UIButton 出现在数字键盘键盘的左下方。

这将是按钮的框架: let dotButton = UIButton(frame: CGRectMake(0, 162, view.frame.width/3, 54)),但必须将其添加为数字键盘键盘的子视图以防止隐藏,该怎么做?

编辑:我让它看起来像这样

    dotButton.addTarget(self, action: "addDot:", forControlEvents: .TouchUpInside)
    dotButton.setTitle(".", forState: .Normal)
    dotButton.titleLabel?.font = UIFont(name: "Helvetica Neue", size: 25)
    dotButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
    dispatch_async(dispatch_get_main_queue(), {
        let keyboardView:UIView = UIApplication.sharedApplication().windows.last?.subviews.first as! UIView
        self.dotButton.frame = CGRectMake(0, keyboardView.frame.size.height-54, self.view.frame.width/3, 54)
        keyboardView.addSubview(self.dotButton)
        keyboardView.bringSubviewToFront(self.dotButton)
    })

但现在我不知道当我点击按钮添加 . 在带有方法 addDot 的 textField 中,我不知道如何告诉 textField 添加一个 . 再次需要帮助...

Edit2:我创建了一个类变量,textFieldInEdit:UITextField! 并且func textFieldShouldBeginEditing(textField: UITextField) -> Bool 我这样做了textFieldInEdit = textField,现在我的函数addDot是:

textFieldWhichEdit.text = "\(textFieldWhichEdit.text).",

但我现在有另一个问题.. 我有不同键盘类型的字段如何检测出现哪个键盘并仅在数字键盘上显示点?

编辑3:

func textFieldDidBeginEditing(textField: UITextField) {
    if(textField.keyboardType.rawValue==4){
        textFieldWhichEdit = textField
        dotButton.hidden = false
    }else{
        dotButton.hidden = true
    }
}

完成我在数字键盘上做了一个点按钮:) 如果有人知道更好的方法可以写它,我会接受答案:)

4

2 回答 2

31

yourTextField.keyboardType = .decimalPad

于 2016-02-08T08:00:56.833 回答
0

将 UITextfieldDelegate 添加到您的控制器,然后再次将您的文本字段链接到一个 IBOulet(例如 tfTest),然后shouldChangeCharactersInRange像这样实现回调。

@IBOutlet weak var tfTest: UITextField!

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    var newText: NSString = textField.text as NSString
    newText = newText.stringByReplacingCharactersInRange(range, withString: string)

    var isEmptyNew = (newText as String).rangeOfString(".", options: NSStringCompareOptions.allZeros, range: nil, locale: nil)?.isEmpty ?? true

    if isEmptyNew {
        //change to decimal pad as we allow to add decimal point now
        textField.keyboardType = UIKeyboardType.DecimalPad

        //refresh responder to change keyboard layout
        textField.resignFirstResponder()
        textField.becomeFirstResponder()
        return true;
    }

    var isEmptyOld = textField.text.rangeOfString(".", options: NSStringCompareOptions.allZeros, range: nil, locale: nil)?.isEmpty ?? true

    //if pressed button is the dot and your text field has already a dot, do not allow this. Normally, this case does not happen cause your keyboard type was changed to number pad before
    if string == "." && !isEmptyOld {
        return false
    } else {
        //show just numbers
        textField.keyboardType = UIKeyboardType.NumberPad
        //refresh responder to change keyboard layout
        textField.resignFirstResponder()
        textField.becomeFirstResponder()

        return true
    }
}

为避免小数分隔符(. 或 ,)出现问题,请使用此代码检索小数分隔符并使用它而不是硬代码值

let df  = NSNumberFormatter()
df.locale = NSLocale.currentLocale()

let decimalPoint = df.decimalSeparator ?? "."
于 2015-04-08T22:12:23.210 回答