Resizing NSView/NSPanel dynamically

Discussion in 'Mac Programming' started by satyam90, Dec 18, 2008.

  1. satyam90 macrumors regular

    satyam90

    Joined:
    Jul 30, 2007
    Location:
    Bangalore, India
    #1
    I inherited MyView class from NSView. In a controllers init method I am creating a panel
    Code:
    	
    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
    Code:
    	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.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    NSAttributedString has a few methods (size, and boundingRectWithSize:eek:ptions:) 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:
     

Share This Page