Re: NSView flickering
- From: Peter Ammon <gershwin@xxxxxxxxxxxxxxx>
- Date: Tue, 15 Nov 2005 03:16:25 -0800
CharlieG wrote:
On 2005-11-14 19:10:34 +0000, matt@xxxxxxxxxxx (matt neuburg) said:
<gambcl@xxxxxxxxxxxxxxxxxxxx> wrote:
It kind of works, but when I resize the window it flickers and worse than that, the text seems to jump up and down in size, when it should be a fixed size.
Any ideas how I can achieve this with a nice smooth window resize?
Best way to get code corrected is to show that code. m.
Sorry, here is the code in question:
- (void)drawRect:(NSRect)aRect { NSScrollView *clipView = (NSScrollView *)[[self superview] superview];
It's a bit safer and more convenient to write [self enclosingScrollView] than the above. Note that your view really is also in a clip view (as in NSClipView), separate from the scroll view.
NSSize clipBounds = [clipView contentSize];
if ([m_displayItems count] > 0) { // Items to display. } else { // Nothing to display, so just print message. NSString *text = @"Some message"; NSFont *font = [NSFont fontWithName:@"Helvetica-Bold" size:64.0]; NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
NSRect textBounds = NSZeroRect;
[textAttrs setObject:font forKey:NSFontAttributeName];
[textAttrs setObject:[NSColor blackColor] forKey:NSForegroundColorAttributeName];
textBounds.size = [text sizeWithAttributes:textAttrs];
textBounds.origin = NSZeroPoint;
NSRect viewBounds = NSZeroRect;
if (textBounds.size.width < clipBounds.width)
{
viewBounds.size.width = clipBounds.width;
textBounds.origin.x = (viewBounds.size.width - textBounds.size.width)/2.0;
}
else
{
viewBounds.size.width = textBounds.size.width;
textBounds.origin.x = 0.0;
}
if (textBounds.size.height < clipBounds.height)
{
viewBounds.size.height = clipBounds.height;
textBounds.origin.y = (viewBounds.size.height - textBounds.size.height)/2.0;
}
else
{
viewBounds.size.height = textBounds.size.height;
textBounds.origin.y = 0.0;
}
[self setBounds:viewBounds];
Eeek! You almost never want to call setBounds:, which changes the view's coordinate system within its frame. Call setFrame: instead.
[self setFrame:viewBounds];
That one is better, but you still shouldn't be doing this from within drawRect:. The usual approach is to use the view's autoresizing mask to have it changes its frame automatically.
// Finally do the drawing. viewBounds = [self bounds]; [[NSColor yellowColor] set]; NSRectFill(viewBounds); [text drawAtPoint:textBounds.origin withAttributes:textAttrs]; } }
I think the text changing size all the time might have something to do with the bounds and frame sizes of the view being
different, even though I set them both to the same value in the code.
When in drawRect:, you think of your bounds origin as being the lower left corner of the frame (or upper left if your view says yes to isFlipped). Calling setBounds: changes the bounds so that drawing at the new bounds origin draws in the lower (or upper) left corner of your view. This is something you almost never want to do.
Any thoughts?
Thanks,
CharlieG.
To draw big text in the center of the window, here's what I'd do.
Create a custom NSView. Make it as large as your window and set its autoresizing mask to have its width and height sizable (so that it always fills the window). Override its hitTest: to always return nil - this will keep the view from capturing mouse clicks.
In your view's drawRect: method, size the text like you're currently doing, and then draw it in the center of the view's bounds (as in, the result of [self bounds]).
That's a cheap cheesy way to do full-window drawing.
I hope some of this helped,
-Peter
-- Pull out a splinter to reply. .
- Follow-Ups:
- Re: NSView flickering
- From: CharlieG
- Re: NSView flickering
- References:
- Re: NSView flickering
- From: matt neuburg
- Re: NSView flickering
- From: CharlieG
- Re: NSView flickering
- Prev by Date: Re: What do you think about Xcode ?
- Next by Date: Bulding with static libraries (Ogg Vorbis)
- Previous by thread: Re: NSView flickering
- Next by thread: Re: NSView flickering
- Index(es):
Relevant Pages
|