2
votes

NSView

I am having problems positioning my NSTextFields inside my views so I hope you guys can help me. I have this NSView *theView with [theView setFrame:NSMakeRect(10, 10, 64, 64)]; which positions the view exactly where I want it.

But now I am trying to add a NSTextField as a subView to theView but something isn't right. Now the image above is how it should be.

Here's my code and what it looks like:

primaryDigit = [[NSTextField alloc] init];
[primaryDigit setFrame:NSMakeRect(4, 6, 60, 58)];
[primaryDigit setFont:[NSFont fontWithName:@"Helvetica Neue" size:55]];
[primaryDigit setBezeled:NO];
[primaryDigit setEditable:NO];
[primaryDigit setSelectable:NO];
[primaryDigit setDrawsBackground:NO];
[primaryDigit setTextColor:[NSColor whiteColor]];
[primaryDigit setStringValue:@"2"];
[self addSubview:primaryDigit];

wrong

And when I do [primaryDigit setFrame:NSMakeRect(0, 0, 64, 64)]; the digit doesn't end up in the left lower corner but it does this:

strange

Is there something fundamental that I don't understand about positioning these views? I'm quite the noobie but I still thought that I understand this. (Btw, the font is different in these images but that shouldn't cause a problem, I think?)

Update: as 'dasdom' asked me to, here's the text field with a background colour backgroundcolor

1
NSTextField is a container. Try to investigate into this by setting the background color of the text view.dasdom
This [self addSubview:primaryDigit]; is in the theView class, right?Alexander
@dasdom I added an image above. The coloured area seems correct but why isn't the text?iMaddin
@Alexander yes 'self' is theViewiMaddin

1 Answers

1
votes

Okay I found out what the problem is.

The text in NSTextFields need "space" around them. So when they have text they don't appear in the bottom left corner of the view but are position a bit away from it.

That also means that if NSTextFields don't have a frame that is large enough then the text may appear cut off.

An example here: two

All the red space above the "2" has to be there otherwise the digit will move off the frame and gets cut off at the bottom.