Live resize bug in NSTextView?
Live resize bug in NSTextView?
- Subject: Live resize bug in NSTextView?
- From: Keith Blount <email@hidden>
- Date: Thu, 17 Feb 2005 13:25:14 -0800 (PST)
- Comment: DomainKeys? See http://antispam.yahoo.com/domainkeys
Hello,
I am just wondering if anybody has come across what
may be a possible bug with NSTextView, and if so,
whether it is a bug or not and whether I am taking the
right approach to it...
I have been having enormous problems setting up a
custom view containing an NSTextView with an
associated margin view for notes. My problem lay in
ensuring that whenever the view was resized, or if
text was entered in the text view or notes were added
to the margin view, the custom view holding all these
subviews would always be exactly the right size to
hold them all snugly.
To do this, I listened for
NSViewFrameDidChangeNotification and tried altering
the sizes of the subviews accordingly. (I have pasted
the message about this problem that I posted a couple
of weeks ago below, including my resizing method.) The
problem was always that sometimes, when I live resized
the view, everything would resize correctly, while
other times, the view would end up too big or too
small for the text it was meant to contain.
Today I noticed that it always resized correctly if
the text view was scrolled to the very top, and always
resized incorreclty if the scrollbar was scrolled to
any other point in the text.
It therefore seems that NSTextView only posts
NSViewFrameDidChangeNotifications during live resize
if the scrollbar is scrolled to the top of the view -
very strange. But this can be seen if you paste a very
long document into TextEdit. Live resize the window
while the scrollbar is at the top of the text view,
and you will see that all the text moves to fit the
new size of the view. If you then scroll to the bottom
of the text view and resize the window, you will
notice that the text does not size to fit until the
resize ends.
To fix this behaviour, I subclassed NSTextView and
overrode -inLiveResize like so:
- (BOOL)inLiveResize
{
BOOL result = [super inLiveResize];
if (result)
[[NSNotificationCenter defaultCenter]
postNotificationName:NSViewFrameDidChangeNotification
object:self];
return result;
}
And voila, my custom view with the margin notes now
seems to resize perfectly with no changes to the code
I posted below.
Is this a bug or expected behaviour? Is my workaround
a decent approach, or am I doing something stupid?
Many thanks to anyone who can give any suggestions -
if there are none, then I at least hope this
information can be of use to somebody.
All the best and sorry for the long post,
Keith
-- Original message --
> Hello,
>
> I have two views within a single scroll view that I
> need to keep in sync. One is a text view, and
another
> is a custom margin view used for keeping notes. To
> keep them together, I can created an NSView subclass
> and added the margin view and text view as subviews
of
> this, and I have set the NSView subclass as the
> document view of the scroll view.
>
> So far, so good. But setting it up so that the view
> resizes itself properly so that it is always just
the
> right height to fit both the text view and the
margin
> view is giving me a real headache, and if anyone can
> give me any advice on how to fix what I am doing, I
> would be very grateful. Currently I am listening for
> NSViewFrameDidChangeNotification in my custom NSView
> subclass. Whenever this notification is posted, I
try
> to recalculate the size of the subviews using the
> method posted below. The trouble is that sometimes
it
> doesn't change size, meaning that there is a lot of
> white space beneath the text view, and sometimes it
> changes size but is too small, so that some of the
> text in the text view is cut off. If anyone can see
> what I am doing wrong and point me in the right
> direction, I would be very grateful.
>
> The method:
>
> // Make sure we can fit the subviews
> -(void)updateSize:(NSNotification *)notification
> {
> id sender = [notification object];
>
> NSClipView *clipView = [[self enclosingScrollView]
> contentView];
>
> // If the scroll view has changed size, or if one
of
> the notes
> // in the margin view has changed size or moved,
make
> sure that
> // the text view is big enough
> if ( (sender == clipView) || ([sender
> isKindOfClass:[HEMMarginCardView class]]) )
> {
> // Note that [marginView minHeight] returns the
> minimum
> // height needed to hold all of the marginal notes
> float textHeight = MAX([marginView
minHeight],[[self
> enclosingScrollView] contentSize].height);
> [textView setMinSize:NSMakeSize(0.0, textHeight)];
> [textView sizeToFit];
> }
>
> // Now make sure margin and self (the document view
> that contains
> // the margin and text view as subviews) is just
big
> enough
> // to hold everything
> if (sender == textView)
> {
> float newHeight = [textView frame].size.height;
>
> NSRect newFrame = [self frame];
> newFrame.size.height = newHeight;
> [self setFrame:newFrame];
>
> newFrame = [marginView frame];
> newFrame.size.height = newHeight;
> [marginView setFrame:newFrame];
> }
> }
>
> Many thanks in advance for any help,
> Keith
__________________________________
Do you Yahoo!?
Yahoo! Mail - Find what you need with new enhanced search.
http://info.mail.yahoo.com/mail_250
_______________________________________________
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