Restrict NSTextField to only allow numbers

前端 未结 9 1553
[愿得一人]
[愿得一人] 2020-12-23 18:31

How do I restrict a NSTextfield to allow only numbers/integers? I\'ve found questions like this one, but they didn\'t help!

相关标签:
9条回答
  • 2020-12-23 18:51

    Swift 2.0 custom formatter with 0 instead of empty space :

    class OnlyIntegerValueFormatter: NSNumberFormatter {
    
        override func isPartialStringValid(partialString: String, newEditingString newString: AutoreleasingUnsafeMutablePointer<NSString?>, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>) -> Bool {
    
            if partialString.isEmpty {
                newString.memory = "0"
                return false
            }
    
            if Int(partialString) < 0 {
                NSBeep()
                return false
            } else {
                return true
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-23 18:56

    In SWIFT, I do it this way

    1. Convert the text value to Int with Int()
    2. Check the converted value is not less than 0
    3. If less than 0, display error message other accept the value

      if ((Int(txtField.stringValue)) < 0){
          // Display error message
      }
      
    0 讨论(0)
  • 2020-12-23 18:59

    [Works with Swift 3.0.1]

    As others suggested, subclass NumberFormatter and override isPartialStringValid method. The easiest way is to drop a NumberFormatter object under your NSTextField in xib/storyboard and update it's Custom Class. Next implementation allows only integers or blank value and plays a beep if string contains illegal characters.

    class IntegerFormatter: NumberFormatter {
    
        override func isPartialStringValid(_ partialString: String, newEditingString newString: AutoreleasingUnsafeMutablePointer<NSString?>?, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
    
            // Allow blank value
            if partialString.numberOfCharacters() == 0  {
                return true
            }
    
            // Validate string if it's an int
            if partialString.isInt() {
                return true
            } else {
                NSBeep()
                return false
            }
        }
    }
    

    String's numberOfCharacters() and isInt() are methods added in an extension.

    extension String {
    
        func isInt() -> Bool {
    
            if let intValue = Int(self) {
    
                if intValue >= 0 {
                    return true
                }
            }
    
            return false
        }
    
        func numberOfCharacters() -> Int {
            return self.characters.count
        }
    }
    
    0 讨论(0)
  • 2020-12-23 19:01

    Here is the steps to create the same....

    Just create the ANYCLASS(called SAMPLE) with sub classing the NSNumberFormatter ...

    in .m file write the following code...

     - (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString      **)newString errorDescription:(NSString **)error {
    // Make sure we clear newString and error to ensure old values aren't being used
    if (newString) { *newString = nil;}
    if (error) {*error = nil;}
    
    static NSCharacterSet *nonDecimalCharacters = nil;
    if (nonDecimalCharacters == nil) {
        nonDecimalCharacters = [[NSCharacterSet decimalDigitCharacterSet] invertedSet] ;
    }
    
    if ([partialString length] == 0) {
        return YES; // The empty string is okay (the user might just be deleting everything and starting over)
    } else if ([partialString rangeOfCharacterFromSet:nonDecimalCharacters].location != NSNotFound) {
        return NO; // Non-decimal characters aren't cool!
    }
    
    return YES;
    

    }

    Now.. in your Actual Class set the formatter to your NSTextField object like below...

    NSTextField *mySampleTxtFld;
    

    for this set the Formatter...

    SAMPLE* formatter=[[SAMPLE alloc]init];// create SAMPLE FORMATTER OBJECT 
    
    self.mySampleTxtFld.delegate=self;
    [self.mySampleTxtFld setFormatter:formatter];
    

    Your done!!!

    0 讨论(0)
  • 2020-12-23 19:04

    Here is a Swift version:

    override func isPartialStringValid(partialString: String, newEditingString newString: AutoreleasingUnsafeMutablePointer<NSString?>, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>) -> Bool {
        if (count(partialString.utf16)) {
            return true
        }
    
        if (partialString.rangeOfCharacterFromSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet) != nil) {
            NSBeep()
            return false
        }
    
        return true
    }
    
    0 讨论(0)
  • 2020-12-23 19:07

    Swift 3 Version

    import Foundation
    
        class OnlyIntegerValueFormatter: NumberFormatter {
    
        override func isPartialStringValid(_ partialString: String, newEditingString newString: AutoreleasingUnsafeMutablePointer<NSString?>?, errorDescription error: AutoreleasingUnsafeMutablePointer<NSString?>?) -> Bool {
    
            // Ability to reset your field (otherwise you can't delete the content)
            // You can check if the field is empty later
            if partialString.isEmpty {
                return true
            }
    
            // Optional: limit input length
            /*
            if partialString.characters.count>3 {
                return false
            }
            */
    
            // Actual check
            return Int(partialString) != nil
        }
    }
    

    Use:

    let onlyIntFormatter = OnlyIntegerValueFormatter()
    myNsTextField.formatter = onlyIntFormatter
    
    0 讨论(0)
提交回复
热议问题