The problem I had with the solutions above is that when I made a change to the textfield in the UI, the cursor would move to the end of the string. So if I had a string in the textfield, like this
[VCOUVER ]
I would place my cursor at 1:
[V|COUVER ]
and type the lower case letter 'a', this would happen:
[VACOUVER| ]
not noticing this, I would type the 'n' and get:
[VACOUVERN| ]
crap... OK, here's my fix:
-(void)controlTextDidChange:(NSNotification *)obj {
if([obj.object isEqualTo:self.locationTextField]) {
NSText *fieldEditor = [[obj userInfo] objectForKey:@"NSFieldEditor"];
NSRange rng = [fieldEditor selectedRange];
[fieldEditor setString:[[fieldEditor string] uppercaseString]];
[fieldEditor setSelectedRange:rng];
}
}
That is, before applying the uppercase, grab the position of the cursor, apply the uppercase, and then put the cursor back.