Default TextField size in NSForm?
Default TextField size in NSForm?
- Subject: Default TextField size in NSForm?
- From: Christian <email@hidden>
- Date: Thu, 23 Nov 2006 13:01:23 +0100
Hi,
I am creating a NSForm programatically in my code and add/remove form
fields dynamically during runtime.
The NSForm is added as content view to a NSBox that was created in
the InterfaceBuilder.
This works fine as long as I hardcode the cellSize and
intercellSpacing of the NSForm and the contentViewMargins of the NSBox.
Is there a method to get the default size of those values (if such a
thing exists)? Maybe something according to Apple's interface
guidelines?
Here is the code:
-(void) awakeFromNib{
NSSize intercellSpacing,cellSize,contentViewMargins;
//get the size of the current NSBox contentView
NSRect boxContentFrame;
boxContentFrame= [[formBox contentView] frame];
//HARDCODE sizes and margins
intercellSpacing= NSMakeSize(1,8);
// the cell size can also be derived from the NSBox contentView size
(by subtracting the margins) .
// But then you have to manually resize the NSBox in IB until
you found a size that looks "good". Is there a default?
cellSize= NSMakeSize(229,22);
contentViewMargins= NSMakeSize(6,8);
//init the NSForm with the size of the NSBox contentView
form= [[[NSForm alloc]initWithFrame:boxContentFrame]autorelease];
//set HARDCODED sizes and margins
[form setIntercellSpacing:intercellSpacing];
[form setCellSize:cellSize];
[formBox setContentViewMargins:contentViewMargins];
//start with one field
NSString *title= [NSString stringWithFormat:@"%d. field name:",[form
numberOfRows]];
[form addEntry:title];
//add the form to the box
[formBox setContentView:form];
}
-(IBAction) addFormField:(id) sender{
//disable textfield selection to prevent strange displaying issues
when a text field is selected during resizing
BOOL windowIsFirstResponder;
windowIsFirstResponder= [formWindow makeFirstResponder:formWindow];
if(! windowIsFirstResponder){
//force deselection
[formWindow endEditingFor: nil];
}
//remember last selection if any
int selectedIndex= [form indexOfSelectedItem];
//add new entry to the field
NSString *title= [NSString stringWithFormat:@"%d. field name:",[form
numberOfRows]];
[form addEntry:title];
//calculate the new cell size
NSFormCell *cell= [form cellAtIndex:0]; //all cells have equal size,
so we take the first one by default
NSSize cellSize= [cell cellSize];
NSSize cellSpacing = [form intercellSpacing];
/*
Expand the window to create space for the new view element.
If the springs were set correctly in the InterfaceBuilder, all
subviews of the window will expand accordingly
*/
[self changeWindowHeight:formWindow byAmount:cellSize.height
+cellSpacing.height];
//recreate the selection if any
[form selectTextAtIndex:selectedIndex];
}
- (void) changeWindowHeight: (NSWindow *) window byAmount: (float)
amount;
{
NSRect oldFrame = [window frame];
NSRect newFrame = oldFrame;
/*
we have to move the origin downwards because in cocoa the origin
is placed at the lower left corner. If we wouldn't move it down,
our form would grow upwards which is very unnatural for a UI.
*/
newFrame.origin.y -= amount;
newFrame.size.height += amount;
//we want to animate the resizing so we use
setFrame:display:animate: instead of setFrame:display:
[window setFrame:newFrame display:YES animate:YES];
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins (at) lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden