Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 528
Michael Avatar asked Dec 22 '22 06:12

Michael


1 Answers

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.

like image 54
anq Avatar answered Jan 17 '23 16:01

anq