Skip to content
This repository has been archived by the owner on Aug 24, 2020. It is now read-only.

[FEATURE] Number TextField #15

Closed
minuscorp opened this issue Aug 31, 2015 · 7 comments
Closed

[FEATURE] Number TextField #15

minuscorp opened this issue Aug 31, 2015 · 7 comments

Comments

@minuscorp
Copy link

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!

@davdroman
Copy link
Owner

Seems completely legit to me. I'll definitely include it in the next alpha 👍

@minuscorp
Copy link
Author

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;
    }

@davdroman
Copy link
Owner

I like that! I'll take some of that validation code if you don't mind.

@minuscorp
Copy link
Author

It's all yours. That's why I posted it 😜

@davdroman
Copy link
Owner

@minuscorp it's done! Check out alpha 4!

@minuscorp
Copy link
Author

Starting tomorrow I'll get this working with the new release!

I'll keep you informed about issues or new features!

Great job!

@davdroman
Copy link
Owner

Alright, I'm closing this now. If there's any problem, don't hesitate to open a new issue 😄

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants