1

I want to create a view with fixed width, but variable height. It means that view should automatically be resized according to its contents height, but, at the same time, it should keep the same width.

How can I achieve that programmatically?

For example, I've got the next piece of code to create a label and a button:

NSTextField *label = [[NSTextField alloc] initWithFrame:[self frame]];
[label setEditable:NO];
[label setBackgroundColor:[NSColor clearColor]];
[label setBezeled:NO];
[label setFont:[NSFont fontWithName:@"Lucida Grande" size:13.0]];
[label setStringValue:@"Sample label text"];

NSButton *button = [[NSButton alloc] initWithFrame:primaryBounds];
[button setBezelStyle:10];
[button setTitle:@"Sample button text"];
[button setBounds:NSInsetRect([button bounds], -8.0, 0)];
[button sizeToFit];

[[self contentView] addSubview:label];
[[self contentView] addSubview:button];

They were set to fill the entire contentView frame. How can I set my label to have fixed width and variable height (based on text contents of itself), and my button to be attached to the bottom of the label?


Okay, I've managed to autosize label like this:

NSTextView *label = [[NSTextView alloc] initWithFrame:NSMakeRect(0, 0, [self frame].size.width, 0)];
[label setEditable:NO];
[label setBackgroundColor:[NSColor clearColor]];
[label setFont:[NSFont fontWithName:@"Lucida Grande" size:13.0]];
[label setString:@"Sample label text"];
[label setHorizontallyResizable:NO];
[label sizeToFit];

1 Answer 1

1

Under Autolayout using Mountain Lion, you would tell the text field what its preferred width should be:

[textField setPreferredMaxLayoutWidth:200]

Now the text field will measure the size of its content as if it wraps at 200 points, and once the content reaches that width, the text field will prefer to grow vertically.

To attach the button to the bottom of the label, you would add a constraint that says the bottom of the label equals the top of the button, plus 22:

[parentView addConstraint:
   [NSLayoutConstraint constraintWithItem:label
                                attribute:NSLayoutAttributeBottom
                                relatedBy:NSLayoutRelationEqual
                                   toItem:button
                                attribute:NSLayoutAttributeTop
                               multiplier:1
                                 constant:22]];

or using the visual format language and standard Aqua spacing:

NSDictionary *viewsDict = NSDictionaryOfVariableBindings(label, button);
[view addConstraints:
     [NSLayoutConstraint constraintsWithVisualFormat:@"V:[label]-[button]"
                                             options:0
                                             metrics:nil
                                               views:viewsDict]];

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.