Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create NSTextField programmatically?

I want to create NSTextField programmatically. I am new to Mac App Development.
Can anyone help me in this ?

Update :
I tried this code

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSView *myView = [[NSView alloc] init];
    NSRect frameRect = NSMakeRect(20,20,100,140);
    NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];
    [myView addSubview:myTextField];
}

This does nothing. Please correct me if i'm wrong anywhere.

Thank you.

like image 364
iUser Avatar asked Dec 16 '25 15:12

iUser


1 Answers

Creating UI elements programmatically is very simple in Cocoa.

It involves two steps:

  1. Create the view and set a frame to it.

  2. Add it as a subview to any super view.

Following snippet will you help you understand better.

NSRect frameRect = NSMakeRect(20,20,40,40); // This will change based on the size you need

NSTextField *myTextField = [[NSTextField alloc] initWithFrame:frameRect];

[myView addSubview:myTextField];
like image 69
Suhas Avatar answered Dec 19 '25 05:12

Suhas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!