How do you limit only 1 decimal entry in a UITextField in Swift for iOS8?

自闭症网瘾萝莉.ら 提交于 2019-12-08 08:59:40

问题


I have searched all around for this but still cannot seem to get it to work. I have a simple iOS program with a UITextField that converts Farenheit to Celsius and Kelvin. I am trying to limit only 1 decimal entry in this UITextField. For example 98.9 is ok but 98..9 is not.

This is the function in which I want to limit only 1 decimal. What I would like is if 1 decimal is entered then it will not allow another 1 to be entered but numbers can still be entered after the first decimal of course.

       func farenheitButton(sender: AnyObject)
      {
        var fVal = self.farenheitTextLabel.text
        var cVal = self.celsiusTextLabel.text
        var kVal = self.kelvinTextLabel.text

        if let fVal = self.farenheitTextLabel.text?.toDouble()
        {
          var fValx = self.farenheitTextLabel.text
          var fDoubleValue : Double = NSString (string: fValx).doubleValue

          var fToC = (fDoubleValue - 32) * (5/9) //convert Farenheit to Celsius formula
          var fToK = ((fDoubleValue - 32) / (1.8000)) + 273.15 //convert Farenheit to Kelvin formula

          //create string from value with a format
          var afToC : Double = fToC
          var bfToC : String = String(format:"%.4f",afToC)

          var afToK : Double = fToK
          var bfToK : String = String(format:"%.4f",afToK)

          celsiusTextLabel.text = bfToC
          kelvinTextLabel.text = bfToK
        }
      }

回答1:


You can set the UITextField.delegate = self and use the textField(textField: UITextField,shouldChangeCharactersInRange range: NSRange,replacementString string: String) method to do what you want.

The following code will not allow you to enter more than 1 decimal point in the textfield.

func textField(textField: UITextField,shouldChangeCharactersInRange range: NSRange,replacementString string: String) -> Bool
{
    let countdots = textField.text.componentsSeparatedByString(".").count - 1

    if countdots > 0 && string == "."
    {
        return false
    }
    return true
}



回答2:


You should add UITextFieldDelegate to class ViewController:

class ViewController: UIViewController, UITextFieldDelegate {
...
}

then

If name of your @IBOutlet is textField, (it could looks like: @IBOutlet weak var textField: UITextField!), then add

override func viewDidLoad() {
        super.viewDidLoad()

        self.textField.delegate = self

        // Do any additional setup after loading the view, typically from a nib.
    }

after that you could use:

func textField(textField: UITextField,shouldChangeCharactersInRange range: NSRange,replacementString string: String) -> Bool
{
    let countdots = textField.text.componentsSeparatedByString(".").count - 1

    if countdots > 0 || textField.text == "."
    {
        return false
    }
    return true
}



回答3:


You can use this subclass of UITextField. It uses the same principle as the answers above, but with a little bit clearer code.

class WMNumberTextField: UITextField, UITextFieldDelegate {

    override func awakeFromNib() {
        super.awakeFromNib()
        delegate = self
    }

    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        guard let text = text else { return false }
        let dots = text.characters.filter { $0 == "." }

        return dots.count == 0 || string != "."
    }

}



回答4:


Swift 3

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

    let countdots =  (textField.text?.components(separatedBy: (".")).count)! - 1

    if (countdots > 0 && string == "."){
        return false
    }
    return true
}



回答5:


I used below code to limit 1 decimal entry in swift 3.0.

let inverseSet = CharacterSet(charactersIn:".0123456789").inverted
let components = string.components(separatedBy: inverseSet)
let filtered = components.joined(separator: "")

if (textField.text?.contains("."))!, string.contains(".") {
   return false
}
return string == filtered



回答6:


Swift 4: This works !

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        guard let counter = textField.text?.components(separatedBy: ".") else { return false }
        if (counter.count - 1 > 0 && string == ".")  { return false }
        return true
    }



回答7:


For swift 5 to restrict text field to enter only one (.) decimal.

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if string == "." {
            let countdots = textField.text!.components(separatedBy: ".").count - 1
            if countdots == 0 {
                return true
            } else {
                if countdots > 0 && string == "." {
                    return false
                } else {
                    return true
                }
            }
        }
        return true
    }


来源:https://stackoverflow.com/questions/27883171/how-do-you-limit-only-1-decimal-entry-in-a-uitextfield-in-swift-for-ios8

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!