12

How to add dot in Number Pad Keyboard? I want when I use notification UIKeyboardDidShowNotification a UIButton to appear over down left side of the Number Pad keyboard.

This will be the frame of the button: let dotButton = UIButton(frame: CGRectMake(0, 162, view.frame.width/3, 54)) , but it has to be added as subview of the number pad keyboard to prevent hiding from it, how will be done ?

Edit: I made it to appear like this

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

but now I don't know how when I click to the button to add . in the textField with method addDot I don't know how to tell to the textField to add a . need help again...

Edit2: I made a class variable which is textFieldInEdit:UITextField! and in func textFieldShouldBeginEditing(textField: UITextField) -> Bool I do this textFieldInEdit = textField and now my function addDot is:

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

but I've got another problem now.. I have fields with different keyboard types how to detect which keyboard appears and show the dot on the Number Pad only ?

Edit3:

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

Done I've made a dot button on Number Pad keyboard :) If someone know better method can write it and I will accept the answer :)

icedwater
  • 4,701
  • 3
  • 35
  • 50
Bogdan Bogdanov
  • 882
  • 11
  • 36
  • 79

2 Answers2

38

yourTextField.keyboardType = .decimalPad

Luis Franco R.
  • 13
  • 1
  • 1
  • 6
AP_
  • 1,013
  • 12
  • 13
0

Add UITextfieldDelegate to your controller, then link you text field again an IBOulet (tfTest for example), then implement the shouldChangeCharactersInRange call-back like this.

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

To avoid problem with the decimal separator (. or ,), use this code to retrieve the decimal separator and use it instead of hard code value

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

let decimalPoint = df.decimalSeparator ?? "."
Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44