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
}
}
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
}
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
}
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 != "."
}
}
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
}
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
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
}