• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Calculating the height of text in NSTextField
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Calculating the height of text in NSTextField


  • Subject: Re: Calculating the height of text in NSTextField
  • From: Philip Dow <email@hidden>
  • Date: Sun, 29 Jan 2006 12:31:40 +0100

Joshua,

There are some posts not a month old which I believe deal exactly with this issue. Douglas Davidson and Aki Inoue address the problem admirably. Search in the archives for a thread titled "Text height for printing (fixed width)". You are probably experiencing the same situation, which apparently has to do with typesetter behavior.

-Phil

On Jan 29, 2006, at 2:12 AM, Joshua Scott Emmons wrote:

Using the instructions I read here: http://developer.apple.com/ documentation/Cocoa/Conceptual/TextLayout/Tasks/StringHeight.html, I worked up a method that I thought would allow me make my NSTextField tall enough to hold all its text.

-(void)resizeTextField:(NSTextField *)aField{
NSSize size = [aField frame].size;
NSTextStorage *textStorage = [[[NSTextStorage alloc] initWithAttributedString:[aField attributedStringValue]]autorelease];
NSTextContainer *textContainer = [[[NSTextContainer alloc] initWithContainerSize:NSMakeSize(size.width, FLT_MAX)] autorelease];
NSLayoutManager *layoutManager = [[[NSLayoutManager alloc]init] autorelease];


[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];
//I've tried 0 for this (suggested by the article) and 2 (the value the field's field editor uses)
[textContainer setLineFragmentPadding:2.0];
[layoutManager glyphRangeForTextContainer:textContainer];
size.height = [layoutManager usedRectForTextContainer:textContainer].size.height;
[aField setFrameSize:size];
}


When I run this, the text field does grow, but not enough. It's shy by about 2px per line, cutting off two whole lines of text at the bottom with your average paragraph of text.

I think I've implemented this is a pretty vanilla fashion and have gone over the docs a few times now. Everything LOOKS right. Can anyone point my mistake out to me?


Cheers, -Joshua Emmons _______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Calculating the height of text in NSTextField (From: Joshua Scott Emmons <email@hidden>)

  • Prev by Date: Re: Warning with NEXT_ROOT environment and libgcc.a
  • Next by Date: Enabling Standard AppleScript Support with Cocoa App
  • Previous by thread: Calculating the height of text in NSTextField
  • Next by thread: Re: Calculating the height of text in NSTextField
  • Index(es):
    • Date
    • Thread