How to limit NSTextField text length and keep it always upper case?
Posted
by carlosb
on Stack Overflow
See other posts from Stack Overflow
or by carlosb
Published on 2009-05-05T21:39:51Z
Indexed on
2010/05/03
2:18 UTC
Read the original article
Hit count: 393
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 through a binding with a validation method but the validation only gets called when the control loses first responder and that's no good.
Temporarly I made it work by observing the notification NSControlTextDidChangeNotification on the text field and having it call the method:
- (void)textDidChange:(NSNotification*)notification {
NSTextField* textField = [notification object];
NSString* value = [textField stringValue];
if ([value length] > 4) {
[textField setStringValue:[[value uppercaseString] substringWithRange:NSMakeRange(0, 4)]];
} else {
[textField setStringValue:[value uppercaseString]];
}
}
But this surely isn't the best way of doing it. Any better suggestion?
© Stack Overflow or respective owner