Search code examples
cocoanstextfieldcapitalization

Force all capitals in NSTextField (Cocoa)


Is there a way to force capitals in a NSTextField?

I want to create a text field for typing in Post Codes, and I want all post codes entered to use capital letters.

e.g. N1 3ET instead of: n1 3et

Also, I am using a regular expression, which only accepts capitals (I know very little about regular expressions, so I'm not keen on modifying it)

Thank you!

Michael


Solution

  • You could give the NSTextField a delegate with something along the lines of:

    - (void)controlTextDidChange:(NSNotification *)aNotification{
        NSText *fieldEditor = [[aNotification userInfo] objectForKey:@"NSFieldEditor"];
        [fieldEditor setString:[[fieldEditor string] uppercaseString]];
    }
    

    It should catch text changing notifications and uppercase the text.