ozmax
ozmax

Reputation: 470

Setting the cell on NSTextField

I have a custom NSTextFieldCell that i want to set on a NSTextField.
If i set it on the IB it works fine.

enter image description here

And this gets it working like this:

enter image description here

But i want to set it programmatically and i try something like this:

-(void)awakeFromNib{

    NSRect theRect = NSRectFromCGRect( NSMakeRect(50, 100, 100, 100));
    NSTextField *inputField = [[NSTextField alloc] initWithFrame:theRect];
    DRKHUDTextFieldCell *theC = [[DRKHUDTextFieldCell alloc] initTextCell:@"textfield"];
    [inputField setCell:theC];

    [[_window contentView] addSubview:inputField];

    }  

This is the result i get:

enter image description here

What is going wrong? Is my code bad or what?

Upvotes: 4

Views: 3337

Answers (2)

adev
adev

Reputation: 2092

Here is a working solution in swift. Above Objective C answer was not complete and hence didn't work for me.

let cell = CustomTextFieldCell(textCell: "")
self.cell = cell
self.isBordered = true
self.backgroundColor = .white
self.isBezeled = true
self.bezelStyle = .squareBezel
self.isEnabled = true
self.isEditable = true
self.isSelectable = true

You need to reset all the properties if you replace cell property. These are default values, try setting your own values matching your case.

Upvotes: 5

PR Singh
PR Singh

Reputation: 653

Try this way, It is working fine:

 NSRect theRect = NSRectFromCGRect( NSMakeRect(50, 100, 100, 100));

    NSTextField *inputField = [[NSTextField alloc] initWithFrame:theRect];

    NSTextFieldCell *theC = [[NSTextFieldCell alloc] initTextCell:@"textfield"];

    [inputField setCell:theC];

    [inputField setBordered:YES];

    [inputField setBackgroundColor:[NSColor whiteColor]];

    [inputField setBezeled:YES];

    [inputField setBezelStyle:0];

   [[_window contentView] addSubview:inputField];

Upvotes: 0

Related Questions