How to limit NSTextField text length and keep it always upper case?

后端 未结 6 1457
无人及你
无人及你 2020-12-01 02:47

Need to have an NSTextField with a text limit of 4 characters maximum and show always in upper case but can\'t figure out a good way of achieving that. I\'ve tried to do it

6条回答
  •  攒了一身酷
    2020-12-01 03:34

    I needed a Formatter to convert to uppercase for Swift 4. For reference I've included it here:

    import Foundation
    
    class UppercaseFormatter : Formatter {
    
        override func string(for obj: Any?) -> String? {
            if let stringValue = obj as? String {
                return stringValue.uppercased()
            }
            return nil
        }
    
        override func getObjectValue(_ obj: AutoreleasingUnsafeMutablePointer?, for string: String, errorDescription error: AutoreleasingUnsafeMutablePointer?) -> Bool {
            obj?.pointee = string as AnyObject
            return true
        }
    }
    

提交回复
热议问题