Re: Metrics for a NSTextField
Re: Metrics for a NSTextField
- Subject: Re: Metrics for a NSTextField
- From: Eric Gorr <email@hidden>
- Date: Wed, 25 Feb 2009 15:19:57 -0500
Hello Martin,
Martin Wierschin wrote:
> Hi Eric,
>
> > NSTextView *fieldEditor = (NSTextView*)[[self window]
fieldEditor:YES
> >
forObject:self];
> > NSRect usedRect = [[fieldEditor layoutManager]
> > usedRectForTextContainer:[fieldEditor
textContainer]];
> >
> > But the used rect returned is empty and [self stringValue] does
contain a
> > string of > 0 characters.
>
> If you check the documentation for the "usedRectForTextContainer:"
> method, it does not trigger any layout. So you'll want to first call
> something like "ensureLayoutForTextContainer:" before asking your
> question.
I thought that was going to work, but unfortunately, it doesn't quite.
What I want to do is (mostly) pictured at:
http://ericgorr.net/cocoadev/outlinetable/namedparts.png
Instead of the black rectangle drawn around the text in the
NSTextField, I would like to draw something akin to what one sees for
a label in a IKImageBrowserView. In order to do that, I need to know
the rectangle of the text in the field.
In order to do this, I subclassed my NSTextField and altered the
drawRect: method to be:
- (void) drawRect:(NSRect)dirtyRect
{
NSTextView *currentEditor = (NSTextView*)[self currentEditor];
NSLog( @"ResourceItemLabel: drawRect: currentEditor: %@",
currentEditor );
if ( currentEditor == nil ) {
NSTextView *fieldEditor = (NSTextView*)[[self window]
fieldEditor:YES forObject:self];
[fieldEditor setString:[self stringValue]];
NSLog( @"ResourceItemLabel: fieldEditor: %@",
fieldEditor );
NSLog( @"ResourceItemLabel: layoutManager: %@",
[fieldEditor layoutManager] );
NSLog( @"ResourceItemLabel: textContainer: %@, %@",
[fieldEditor textContainer],
NSStringFromSize( [[fieldEditor textContainer]
containerSize] ) );
[[fieldEditor layoutManager]
ensureLayoutForTextContainer:[fieldEditor textContainer]];
NSRect usedRect = [[fieldEditor layoutManager]
usedRectForTextContainer:[fieldEditor textContainer]];
NSLog( @"usedRectForTextContainer: %@, %@",
NSStringFromRect( usedRect ), [self stringValue] );
[[NSColor blueColor] setFill];
[NSBezierPath fillRect:usedRect];
}
[super drawRect:dirtyRect];
}
I only want to do this special drawing when the user isn't editing the
label and asking for the currentEditor allows me to do that.
The output when it draws the label is:
2009-02-25 14:35:32.146 OutlineCollection-VB[69495:813]
ResourceItemLabel: drawRect: currentEditor: (null)
2009-02-25 14:35:37.445 OutlineCollection-VB[69495:813]
ResourceItemLabel: fieldEditor: <NSTextView: 0x36f1d0>
Frame = {{0.00, 0.00}, {0.00, 0.00}}, Bounds = {{0.00, 0.00},
{0.00, 0.00}}
Horizontally resizable: NO, Vertically resizable: YES
MinSize = {0.00, 0.00}, MaxSize = {0.00, 10000000.00}
2009-02-25 14:35:38.809 OutlineCollection-VB[69495:813]
ResourceItemLabel: layoutManager: <NSLayoutManager: 0x36fe10>
1 containers, text backing has 8 characters
selected character range {8, 0} affinity: upstream granularity:
character
marked character range {8, 0}
Currently holding 8 glyphs.
Glyph tree contents: 8 characters, 8 glyphs, 1 nodes, 32 node
bytes, 0 storage bytes, 32 total bytes, 4.00 bytes per character, 4.00
bytes per glyph
Layout tree contents: 8 characters, 8 glyphs, 0 laid glyphs, 0
laid line fragments, 1 nodes, 32 node bytes, 0 storage bytes, 32 total
bytes, 4.00 bytes per character, 4.00 bytes per glyph, 0.00 laid
glyphs per laid line fragment, 0.00 bytes per laid line fragment
2009-02-25 14:35:44.824 OutlineCollection-VB[69495:813]
ResourceItemLabel: textContainer: <NSTextContainer: 0x370530>, {0, 1e
+07}
2009-02-25 14:35:46.633 OutlineCollection-VB[69495:813]
usedRectForTextContainer: {{0, 0}, {0, 0}}, nacreous
The usedRect empty. I believe the reason why is because the
fieldEditor that the window has given me has a zero width.
Eventually, I will want to use usedRect to determine the height of the
label, so it is not a solution to simply do:
[[NSColor blueColor] setFill];
[NSBezierPath fillRect:[self bounds]];
I did just spot boundingRectForGlyphRange:inTextContainer: which
apparently will force the layout to occur, saving me from needing to
call ensureLayoutForTextContainer: myself. However, until I can get a
valid field editor for the NSTextField, I'm stuck.
If anyone has a suggestion on how to better accomplish this task, I am
interested.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden