数字输入在文本字段中

时间:2015-05-09 09:56:40

标签: swift

其他问题:

我的代码仍需要一些帮助。文本字段是“测量值”'我打算有30个不同的texfields(measuredValue1 ... 30)。当我输入' 923'该文本将设置为' 9.23'马上。然后我想添加' ...' 92.34'但那不起作用。谢谢你的帮忙。

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

    if count(string) == 0 { return true }

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

    switch textField {

    case digitsOnlyTextField:

        if count(measuredValue) == 3  || count(measuredValue) == 4 {
            let stringNumeric = Double(measuredValue.toInt()!) / 100
            measuredValue = String(format:"%.2f", stringNumeric)
            digitsOnlyTextField.text = measuredValue
        }

        return measuredValue.containsOnlyCharactersIn("0123456789") && count(measuredValue) <= 4

    default:
        return true
    }

}

原始问题:

我想验证我的文字字段,以便为我的应用获得正确的输入。输入需要格式化为&#39; 9.90&#39;或者&#39; 15.34&#39;。所以总是3或4位数,总是2位小数。

我想使用&#39;数字键盘&#39; (仅0 ... 9,无点)并在用户退出字段后添加小数点。因此用户输入为990或1534,然后在文本字段中自动变为9.90或15.34。

我首先尝试搜索示例,但没有找到我要找的内容。 任何帮助赞赏。 扬

2 个答案:

答案 0 :(得分:0)

您必须实施UITextField委托方法

func textFieldDidEndEditing(textField: UITextField) {
    //Logic goes here....
    var textString = textField.text as! String
    let textLength = countElements(textString)
    if textLength >= 3 && textLength <= 4 {
        let stringNumeric = Double(textString.toInt()!) / 100
        let texts = String(format:"%.2f", stringNumeric)
        textField.text = texts
    }
}

您的班级应确认协议UITextFieldDelegate

答案 1 :(得分:0)

这是我使用的'最终'代码(但我愿意接受改进!)。一些评论:

  1. 当用户点击背景(并离开文本字段)时 小数点设置。
  2. 当用户复制&amp;从另一个应用程序粘贴文本,此代码处理该文本。
  3. 当用户返回文本字段进行编辑时,小数点会再次设置。
  4. 正确处理删除值(到空文本字段)。
  5. 所以我很高兴。谢谢你的帮助。

    import UIKit
    
    class ViewController: UIViewController, UITextFieldDelegate {
    
    @IBOutlet weak var numberField: UITextField!
    
      override func viewDidLoad() {
    super.viewDidLoad()
    numberField.delegate = self
    numberField.keyboardType = UIKeyboardType.NumberPad
    }
    
    override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    }
    
    // Tap background to add decimal point and defocus keyboard
    
    @IBAction func userTappedBackground(sender: AnyObject) {
    for view in self.view.subviews as! [UIView] {
      if let textField = view as? UITextField {
        if count(numberField.text) > 0 {
            var numberString = numberField.text
            numberString = numberString.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
            var numberFromString = Double(numberString.toInt()!) / 100
            numberField.text = String(format:"%.2f", numberFromString)
        }
        textField.resignFirstResponder()
      }
    }
    }
    
    func textField(textField: UITextField,
        shouldChangeCharactersInRange range: NSRange,
        replacementString string: String)
        -> Bool {
            var result = true
            var prospectiveText = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
            prospectiveText = prospectiveText.stringByReplacingOccurrencesOfString(".", withString: "", options: NSStringCompareOptions.LiteralSearch, range: nil)
            if textField == numberField {
                if count(string)>0 {
                    let disallowedCharacterSet = NSCharacterSet(charactersInString: "0123456789").invertedSet
                    let replacementStringIsLegal = string.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
                     let resultingStringLengthIsLegal = count(prospectiveText) <= 4
                    let scanner = NSScanner(string: prospectiveText)
                    let resultingTextIsNumeric = scanner.scanDecimal(nil) && scanner.atEnd
                    result = replacementStringIsLegal && resultingStringLengthIsLegal && resultingTextIsNumeric
                }
            }
            return result
    }