-
Notifications
You must be signed in to change notification settings - Fork 83
[FEATURE] Number TextField #15
Comments
Seems completely legit to me. I'll definitely include it in the next alpha 👍 |
Hi! I've come with maybe a solution I make a extension of the String class (sorry for being in Swift...) which checks if the text only contains numbers or if it's even a number (doesn't check if they're negative, which could've another nice feature or if it's decimal o int) extension String{
func containsOnlyCharactersIn(matchCharacters: String) -> Bool {
let disallowedCharacterSet = NSCharacterSet(charactersInString: matchCharacters).invertedSet
return self.rangeOfCharacterFromSet(disallowedCharacterSet) == nil
}
func isNumeric() -> Bool
{
let scanner = NSScanner(string: self)
scanner.locale = NSLocale.currentLocale()
return scanner.scanDecimal(nil) && scanner.atEnd
}
} Which is used in the BOTextTableViewCell section.addCell(BOTextTableViewCell(title: "Weight", key: "weight", handler: ({
(cell: AnyObject!) in
if let cell_ : BOTextTableViewCell = cell as? BOTextTableViewCell{
cell_.minimumTextLength = 2
cell_.textField.keyboardType = UIKeyboardType.NumbersAndPunctuation
cell_.textField.delegate = self
cell_.inputErrorBlock = {
(cell: BOTextTableViewCell!, error: BOTextFieldInputError) -> Void in
self.presentAlertControllerWithTitleAndMessage("Error", message: "Value too short")
}
}
}))) And the UITextFieldDelegate function checks on typing whether is a number or not: func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let prospectiveText = ((textField.text ?? "") as NSString).stringByReplacingCharactersInRange(range, withString: string)
return prospectiveText.isNumeric()
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true;
} |
I like that! I'll take some of that validation code if you don't mind. |
It's all yours. That's why I posted it 😜 |
@minuscorp it's done! Check out alpha 4! |
Starting tomorrow I'll get this working with the new release! I'll keep you informed about issues or new features! Great job! |
Alright, I'm closing this now. If there's any problem, don't hesitate to open a new issue 😄 |
Hi there!
I'm working now with your library and I've come with a feature that could be an extension for BOTextViewCell which only accepts int numbers or decimal numbers on it. Do you think is viable? Or it's reachable with the current library?
Regards!
The text was updated successfully, but these errors were encountered: