Bog*_*nov 4 keyboard swift numpad
如何在数字键盘中添加点?我希望当我使用通知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)
})
Run Code Online (Sandbox Code Playgroud)
但现在我不知道当我点击按钮添加时.在textField中使用方法addDot我不知道如何告诉textField添加一个.再次需要帮助......
Edit2:我创建了一个类变量,textFieldInEdit:UITextField!
在func textFieldShouldBeginEditing(textField: UITextField) -> Bool
我这样做textFieldInEdit = textField,现在我的函数addDot是:
textFieldWhichEdit.text = "\(textFieldWhichEdit.text)." ,
但我现在有另一个问题..我有不同键盘类型的字段如何检测出现哪个键盘并仅在数字键盘上显示点?
EDIT3:
func textFieldDidBeginEditing(textField: UITextField) {
if(textField.keyboardType.rawValue==4){
textFieldWhichEdit = textField
dotButton.hidden = false
}else{
dotButton.hidden = true
}
}
Run Code Online (Sandbox Code Playgroud)
完成我在数字键盘上做了一个点按钮:)如果有人知道更好的方法可以写它,我会接受答案:)
AP_*_*AP_ 22
yourTextField.keyboardType = .decimalPad
将 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
}
}
Run Code Online (Sandbox Code Playgroud)
为了避免小数分隔符(. 或 ,)出现问题,请使用此代码检索小数分隔符并使用它代替硬代码值
let df = NSNumberFormatter()
df.locale = NSLocale.currentLocale()
let decimalPoint = df.decimalSeparator ?? "."
Run Code Online (Sandbox Code Playgroud)