NSTextField Size

  • Hi,

    This should be easy, but after hours looking, I need some advice!!

    I'm trying to put up an alert in a custom window (NSAlert won't work!)
    - The alert message could be any length, but for the life of me I
    can't figure out how to resize the label to the size of the text.

    I've set up the window so that when resized the textbox grows with it
    - so If I could just get the height I need, I could resize the window,
    but I can't get the height of the text.

    Many thanks in advance!!

    Ben.
  • Have you looked at -sizeToFit?
    (I don't know how well this will work for multi-line strings… one issue
    you're up against is that there really isn't one "correct" result. It
    depends on how wide you're willing to let the string go.)

    Ben Robinson wrote:
    > Hi,
    >
    > This should be easy, but after hours looking, I need some advice!!
    >
    > I'm trying to put up an alert in a custom window (NSAlert won't work!)
    > - The alert message could be any length, but for the life of me I
    > can't figure out how to resize the label to the size of the text.
    >
    > I've set up the window so that when resized the textbox grows with it
    > - so If I could just get the height I need, I could resize the window,
    > but I can't get the height of the text.
    >
    > Many thanks in advance!!
    >
    >
    > Ben.
  • > I'm trying to put up an alert in a custom window (NSAlert won't work!)
    > - The alert message could be any length, but for the life of me I
    > can't figure out how to resize the label to the size of the text.

      It's not clear whether you want a text field that sizes only in one
    dimension or if it's okay that it sizes both height and / or width as
    needed. If the latter, the -sizeToFit method is your best bet.

      If the former, this is a FAQ (please search the archives before
    posting) which usually leads to one of these:

    http://www.cocoadev.com/index.pl?CCDGrowingTextField
    http://www.cocoadev.com/index.pl?IFVerticallyExpandingTextField

    --
    I.S.
  • >> I'm trying to put up an alert in a custom window (NSAlert won't
    >> work!)
    >> - The alert message could be any length, but for the life of me I
    >> can't figure out how to resize the label to the size of the text.
    >
    > It's not clear whether you want a text field that sizes only in one
    > dimension or if it's okay that it sizes both height and / or width as
    > needed. If the latter, the -sizeToFit method is your best bet.
    >
    > If the former, this is a FAQ (please search the archives before
    > posting) which usually leads to one of these:
    >
    > http://www.cocoadev.com/index.pl?CCDGrowingTextField
    > http://www.cocoadev.com/index.pl?IFVerticallyExpandingTextField
    >
    > --
    > I.S.

    I've started to have a good look at those links and am trying to
    extract the stuff I need, I suppose I could try and use it complete,
    but they seem over complicated!!

    The window will keep the same width, just grow in height, also my text
    box is a none editable label that I need to size as the alert opens,
    and only then (as apposed to growing as it's edited) - So I'm thinking
    (hoping) it should be possible it a couple of lines!

    Thanks.

    Ben.
  • > I've started to have a good look at those links and am trying to
    > extract the stuff I need, I suppose I could try and use it
    > complete, but they seem over complicated!!

    This should do the trick:

    NSRect bounds = NSMakeRect( 0, 0, [textField frame].size.width,
    99999 );
    [textField setStringValue:message];
    float requiredHeight = [[textField cell]
    cellSizeForBounds:bounds].height;

    (untested, typed in Mail)

    ~Martin
  • >> I've started to have a good look at those links and am trying to
    >> extract the stuff I need, I suppose I could try and use it
    >> complete, but they seem over complicated!!
    >
    > This should do the trick:
    >
    > NSRect bounds = NSMakeRect( 0, 0, [textField frame].size.width,
    > 99999 );
    > [textField setStringValue:message];
    > float requiredHeight = [[textField cell]
    > cellSizeForBounds:bounds].height;
    >
    > (untested, typed in Mail)
    >
    > ~Martin
    >

    That was exactly what I needed - thanks!

    I was stuggling to extract the code from the links,  I think the fact
    the height is a float got in the way?!?!  Seems odd to me, but works!

    Just for completeness, my final code, which resizes the window, and
    catches short strings to keep the window above a minimum height:
    NSRect bounds = NSMakeRect( 0, 0, [alertMessage frame].size.width,
    99999 );
    [textField setStringValue:message];
    float requiredHeight = [[alertMessage cell]
    cellSizeForBounds:bounds].height;
    if (requiredHeight < 52) requiredHeight = 52;
    [theDialogWindow setContentSize:NSMakeSize(423, requiredHeight+106)];

    Many thanks

    Ben.
  • Why do you switch between "[alertMessage frame].size.width" (good!) and
    a hard-coded "423" (bad)?

    Ben Robinson wrote:
    >>> I've started to have a good look at those links and am trying to
    >>> extract the stuff I need, I suppose I could try and use it complete,
    >>> but they seem over complicated!!
    >>
    >> This should do the trick:
    >>
    >> NSRect bounds = NSMakeRect( 0, 0, [textField frame].size.width,
    >> 99999 );
    >> [textField setStringValue:message];
    >> float requiredHeight = [[textField cell]
    >> cellSizeForBounds:bounds].height;
    >>
    >> (untested, typed in Mail)
    >>
    >> ~Martin
    >>
    >
    >
    > That was exactly what I needed - thanks!
    >
    > I was stuggling to extract the code from the links,  I think the fact
    > the height is a float got in the way?!?!  Seems odd to me, but works!
    >
    > Just for completeness, my final code, which resizes the window, and
    > catches short strings to keep the window above a minimum height:
    > NSRect bounds = NSMakeRect( 0, 0, [alertMessage frame].size.width,
    > 99999 );
    > [textField setStringValue:message];
    > float requiredHeight = [[alertMessage cell]
    > cellSizeForBounds:bounds].height;
    > if (requiredHeight < 52) requiredHeight = 52;
    > [theDialogWindow setContentSize:NSMakeSize(423, requiredHeight+106)];
    >
    > Many thanks
    >
    > Ben.
  • > if (requiredHeight < 52) requiredHeight = 52;

    It might also be a good idea to limit the maximum height based on the
    current screen frame.

    ~Martin
  • The window will always be the same width, hence the fixed number, but
    fair point, I'll change it!! :-)
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today