Commit all capitals to NSTextField (Cocoa)

Is there a way to force capitals into an NSTextField?

I want to create a text box for entering postal codes, and I want all postal codes to be entered in capital letters.

eg. N1 3ET instead: n1 3et

In addition, I use a regular expression that only accepts capitals (I know little about regular expressions, so I don’t want to change it)

Thank!

Michael

+3
source share
6 answers

You can provide an NSTextField delegate with something like:

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

It should catch notifications of text changes and write the text in uppercase.

+6
source

Anq , Cocoa , NSFormatter. ( ).

. Cocoa.

+2

. , NSTextField, :

override func controlTextDidChange(obj: NSNotification) 
{
    if let info = obj.userInfo, text = info["NSFieldEditor"] as? NSText,
        string = text.string
    {
        text.string = string.uppercaseString
    }
}
+2

, Anq Swift

override func controlTextDidChange(obj: NSNotification!) {
    //Make the username all caps to match the PM
    var infoDictionary:Dictionary = obj.userInfo! as Dictionary
    var text:NSText = infoDictionary["NSFieldEditor"] as NSText;
    text.string = text.string.uppercaseString
}
0

NStexfield IB, :

- (void)controlTextDidChange:(NSNotification *)aNotification{
  editField = [aNotification object];
  NSString* value = [editField stringValue];
  [editField setStringValue:[value uppercaseString]];
  //NSLog(@"editField%@", editField.stringValue);

}
0

, , , , . ,

[VCOUVER]

1:

[V | COUVER]

"a", :

[VACOUVER | ]

, "n" :

[VACOUVERN | ]

... , :

-(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];
    }
}

, , , , .

0

Source: https://habr.com/ru/post/1727894/