Reputation: 470
I have a custom NSTextFieldCell
that i want to set on a NSTextField
.
If i set it on the IB it works fine.
And this gets it working like this:
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:
What is going wrong? Is my code bad or what?
Upvotes: 4
Views: 3337
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
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