PDA

View Full Version : Resizing NSView/NSPanel dynamically




satyam90
Dec 18, 2008, 09:53 AM
I inherited MyView class from NSView. In a controllers init method I am creating a panel

NSPanel *panel = [[[NSPanel alloc] initWithContentRect:NSMakeRect( 0., 0., 270., 65. )
styleMask:NSBorderlessWindowMask
backing:NSBackingStoreBuffered defer:NO] autorelease];
.
.
.
.
MyView *view = [[[KABubbleWindowView alloc] initWithFrame:[panel frame]] autorelease];
[view setTarget:self];
[view setAction:@selector( _bubbleClicked: )];
[panel setContentView:view];
.
.
.
.
return ( self = [super initWithWindow:panel] );


Later in one of the methods I am setting the title, icon and text as follows

id ret = [[[self alloc] init] autorelease];
[ret setTitle:title];
if( [text isKindOfClass:[NSString class]] ) [ret setText:text];
else if( [text isKindOfClass:[NSAttributedString class]] ) [ret setAttributedText:text];
[ret setIcon:icon];
return ret;

Its working fine upto this point. But my problem is that if title is more than the width of panel ie 270 pixels or if text height more than 65 pixels, the text is crossing the border and not able to view it. No scroll bars are to be there in that. It's a restriction.
So, I want to increase the size of view & panel dynamically depending on width and height of the text. What's the procedure to achieve this.



kainjow
Dec 18, 2008, 04:39 PM
NSAttributedString has a few methods (size, and boundingRectWithSize:options:) which can help you determine if the text field/view with a given string is too large. Then you can size the views and panel via setFrame: