Fatmah Fatmah - 7 months ago 19
Swift Question

The number of digits on textField

I'm working on my app and I need help on

UITextField
.

I want to make the number of digits that appear to be specific (e.g. 3) knowing that the numbers shown on the
UITextField
is from the value of the slider between the range of 1 to 0 (I need this range,) with numbers between them (0.5 , 0.6 ...).

The numbers I am getting is 6 digits (0.123456) and I would like it to show less than that.

I have tried this funcion after added
UITextFieldDelegate
:

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


But it's not working. I got help from some one to use this function:

@IBAction func testField(sender: UITextField) {
let TheLength = sender.text!.characters.count
if TheLength = >= 3 {
let TheRange = Range(start: sender.text!.startIndex, end: sender.text!.startIndex.advancedBy(3))
var newdi = sender.text!.substringWithRange(TheRange)
sender.text = newdi
}
}


Also nothing changed.

Note: I have more
UITextFields
to fix.

Answer

Use it something like this.

textField.text = String(format: "%0.1f", slider.value)
Comments