• 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
Calculating the height of text in NSTextField
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Calculating the height of text in NSTextField


  • Subject: Calculating the height of text in NSTextField
  • From: Joshua Scott Emmons <email@hidden>
  • Date: Sat, 28 Jan 2006 19:12:54 -0600

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
  • Follow-Ups:
    • Re: Calculating the height of text in NSTextField
      • From: Philip Dow <email@hidden>
  • Prev by Date: Using Evan Schoemberg's Custom tabs
  • Next by Date: is kMDItemTextContent case-insensitive?
  • Previous by thread: Re: PSMTabBarControl (was: Using Evan Schoemberg's Custom tabs)
  • Next by thread: Re: Calculating the height of text in NSTextField
  • Index(es):
    • Date
    • Thread