In swift, I am trying to make a text field that will allow a button to be enabled, but only when the text field contains an integer. How can I do this?
Here's a reusable method for Cocoa, swift 4.1
1- Create a file with the class NSTextFieldFormated below
2- In the 'Identity inspector' change the 'CustomClass class' of the field to be formatted to NSTextFieldFormated
3- In the 'Attributes inspector' define the 'int Min Value', 'int Max Value' and 'int Length' of the field to be formatted
4- Drag and drop an 'Object' (NSObject) from the 'Object library' to the 'View Controller Scene' in the outline interface builder objects listing (left of the interface drawing area)
5- In the 'Identity inspector' set the 'CustomClass class' to NSTextFieldFormated
6- Select the field to be formatted and in the Connections inspector set the field delegate to the new 'Text Field Formatted' object in 'View Controller Scene'.
Then the NSTextField is now formatted.
class NSTextFieldFormated: NSTextField, NSControlTextEditingDelegate {
private struct Digit {
var minValue: Int? // minimum
var maxValue: Int? // maximum
var digitAmt: Int? // minimumIntegerDigits
}
private var digit = Digit()
//-Integer
@IBInspectable var intMinValue: Int {
get {
guard let minVal = digit.minValue else { return Int.min }
return minVal
}
set { digit.minValue = newValue ; setFormatter() }
}
@IBInspectable var intMaxValue: Int {
get {
guard let maxVal = digit.maxValue else { return Int.max }
return maxVal
}
set { digit.maxValue = newValue ; setFormatter() }
}
@IBInspectable var intLenght: Int {
get {
guard let length = digit.digitAmt else { return -1 }
return length
}
set { digit.digitAmt = newValue ; setFormatter() }
}
private func setFormatter() {
let lformatter = IntegerFormatter()
lformatter.minimum = intMinValue as NSNumber
lformatter.maximum = intMaxValue as NSNumber
if intLenght != -1 {
lformatter.minimumIntegerDigits = intLenght
}
self.formatter = lformatter
}
class IntegerFormatter: NumberFormatter {
override func isPartialStringValid(_ partialString: String,
newEditingString newString: AutoreleasingUnsafeMutablePointer?,
errorDescription error: AutoreleasingUnsafeMutablePointer?) -> Bool {
if partialString.rangeOfCharacter(from: CharacterSet.decimalDigits.inverted) != nil {
NSSound.beep()
return false
}
return Int(partialString) != nil
}
}
override func controlTextDidChange(_ notification: Notification) {
if let textField = notification.object as? NSTextFieldFormated {
if let val = Int(textField.stringValue) {
//print(">4")
if val > textField.intMaxValue {
textField.stringValue = String(textField.intMaxValue)
}
}
}
}
}