Saturday, November 8, 2008

Cocoa: Horizontal Box

It's a couple of days that I'm searching for something like QHBoxLayout (Qt Horizonal Box Layout) in Cocoa. There's NSMatrix that do something like this... but it uses NSCell and it sets the same size for all the cells, so this is not what I want. My solution is a simple NSView subclass...

And This is a simple "Main" to test the HBox.
- (NSTextField *)createTextField:(NSString *)text {
  NSTextField *field = [[NSTextField alloc] init];
  [field setStringValue:text];
  return [field retain];

- (NSButton *)createButton:(NSString *)caption {
  NSButton *button = [[NSButton alloc] init];
  [button setBezelStyle:NSRecessedBezelStyle];
  [button setButtonType:NSPushOnPushOffButton];
  [[button cell] setHighlightsBy:(NSCellIsBordered | NSCellIsInsetButton)];
  [button setShowsBorderOnlyWhileMouseInside:YES];
  [button setButtonType:NSOnOffButton];
  [button setTitle:caption];

  return [button retain];

- (void)awakeFromNib {
  [box addItem:[self createTextField:@"Label 1"]];
  [box addItem:[self createButton:@"Button 1"]];
  [box removeItemAtIndex:0];
  [box addItem:[self createTextField:@"Label 2"]];
  [box addItem:[self createButton:@"Say"]];

Download Here the Test Cocoa HBox Source.

No comments:

Post a Comment