Calculating the height of text in NSTextField
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