Re: Calculating the height of text in NSTextField
Re: Calculating the height of text in NSTextField
- Subject: Re: Calculating the height of text in NSTextField
- From: Matt Neuburg <email@hidden>
- Date: Sat, 28 Jan 2006 18:43:47 -0800
- Thread-topic: Calculating the height of text in NSTextField
On Sat, 28 Jan 2006 19:12:54 -0600, Joshua Scott Emmons <email@hidden>
said:
>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?
Now that we have boundingRectWithSize:..., all that work shouldn't be
necessary. I'm curious whether you get any better answer that way? m.
--
matt neuburg, phd = email@hidden, <http://www.tidbits.com/matt/>
A fool + a tool + an autorelease pool = cool!
AppleScript: the Definitive Guide
<http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt>
_______________________________________________
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