Ive been trying to achieve a percentage mask for a textfield that starts with the value 0.00%. If a user taps '1' it will become 0.01%. If they then tap 0 it becomes 0.10% and so on.
I've achieved this already with currency. And I've come very close using percentage but not managed to create exactly what I'm after.
So, field is originally set to 0.00%. I'd like to display the percentage symbol, I'd like the max number to be 100.00%, I'd like the max decimal places to be 2, and like the field to be updated when the user types and I'd like them to be able to press backspace to delete the last number entered. Oh and also when it reaches the maximum of 100.00, pressing another number does not reset the value, or do anything screwy.
Here is the code I've tried. I've also tried MANY variations:
func textFieldDidChanged(textField: UITextField) {
numberFormatter.numberStyle = .PercentStyle
numberFormatter.maximumFractionDigits = 2
numberFormatter.minimumFractionDigits = 2
numberFormatter.maximum = 100
let text = textField.text!.stringByReplacingOccurrencesOfString(numberFormatter.percentSymbol, withString: "").stringByReplacingOccurrencesOfString(numberFormatter.groupingSeparator, withString: "").stringByReplacingOccurrencesOfString(numberFormatter.decimalSeparator, withString: "")
textField.text = numberFormatter.stringFromNumber((text as NSString).doubleValue / 100.0)
}
I've looked all over, and spent way too long on this. Please help.