views:

158

answers:

2

Hi, 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

+4  A: 

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.

anq
+1  A: 

Anq's answer works well, but in the Cocoa world, this is what NSFormatter was created for. It's about the same amount of work and can be easily ported to any part of your project (or to any other project).

See also the Data Formatting Programming Guide For Cocoa.

Joshua Nozzi