davdroman / Bohr

Settings screen composing framework
MIT License
1.26k stars 83 forks source link

[FEATURE] Number TextField #15

Closed minuscorp closed 9 years ago

minuscorp commented 9 years ago

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 commented 9 years ago

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

minuscorp commented 9 years ago

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 commented 9 years ago

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

minuscorp commented 9 years ago

It's all yours. That's why I posted it :stuck_out_tongue_winking_eye:

davdroman commented 9 years ago

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

minuscorp commented 9 years ago

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

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

Great job!

davdroman commented 9 years ago

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