NSLayoutManager causing crash in resizeSubviewsWithOldSize?
NSLayoutManager causing crash in resizeSubviewsWithOldSize?
- Subject: NSLayoutManager causing crash in resizeSubviewsWithOldSize?
- From: Keith Blount <email@hidden>
- Date: Mon, 3 Apr 2006 01:16:59 -0700 (PDT)
Hi,
Sorry to repost this, but I've been stuck on this for
two or three days now, with no joy, so I've updated
this with a few more details in the hope that there is
someone out there who missed my earlier post but can
help. :)
I am trying to implement an editable multi-column text
view much like the (read-only) view in Tofu
(http://homepage.mac.com/asagoo/tofu/prerelease/index.html).
The idea is that I have a custom view containing
multiple text views as sub-views arranged
side-by-side, which can be resized vertically
(NSViewHeightSizable flag) but have a fixed width.
This is contained in a scroll view so that the user
can scroll horizontally to view the various columns. I
have used the TextSizingExample and TextEdit source as
a basis for this.
As in TextEdit's multiple page view, I use
NSLayoutManager's delegate method,
-layoutManager:didCompleteLayoutForTextContainer:atEnd:,
to determine whether I need to add or remove columns.
In fact, I use TextEdit's method without any changes:
- (void)layoutManager:(NSLayoutManager *)lm
didCompleteLayoutForTextContainer:(NSTextContainer*)textContainer
atEnd:(BOOL)layoutFinishedFlag {
//if ([self hasMultiplePages]) {
NSArray *containers = [layoutManager
textContainers];
if (!layoutFinishedFlag || (textContainer ==
nil)) {
// Either layout is not finished or it is
but there are glyphs laid nowhere.
NSTextContainer *lastContainer =
[containers lastObject];
if ((textContainer == lastContainer) ||
(textContainer == nil)) {
// Add a new page if the newly full
container is the last container or the nowhere
container.
// Do this only if there are glyphs
laid in the last container (temporary solution for
3729692, until AppKit makes something better
available.)
if ([layoutManager
glyphRangeForTextContainer:lastContainer].length > 0)
[self addPage];
}
} else {
// Problem whereby pages are getting
removed here at the same time as they are being
resized?
// Layout is done and it all fit. See if
we can axe some pages.
unsigned lastUsedContainerIndex =
[containers indexOfObjectIdenticalTo:textContainer];
unsigned numContainers = [containers
count];
while (++lastUsedContainerIndex <
numContainers) {
[self removePage];
}
}
// }
}
My own -addPage and -removePage methods do exactly the
same as TextEdit's - they add a text view and
container to the hierarchy - the only difference being
their placement (they are arranged horizontally - and
should be renamed to -addColumn and -removeColumn when
I tidy up the code).
So far, so good - everything works as expected in that
text gets laid out in columns that can be resized
vertically, and columns get added or removed
automatically as desired - mostly. However, I have two
problems, one of which is spewing an exception (and
sometimes crash), the other being just irritating:
1) Unlike TextEdit's static pages, my columns can be
resized vertically, which means that columns need
creating and removing dynamically during resize. They
are resized whenever the superview is resized, which
means that the layout manager's delegate method gets
called and tries to add or remove columns (subviews)
during a live resize. This causes NSCFArray "out of
bounds" exceptions in -resizeSubviewsWithOldSize: (or
at least, this seems to be the method causing the
grief when I breakpoint on -[NSException raise]). This
occurs when removing columns, when resizing the view
to make it smaller immediately after resizing it to
make it bigger. As I understand it, what seems to be
happening is that -resizeSubviewsWithOldSize: is still
trying to resize subviews that have just been removed
in the NSLayoutManager delegate method. But if I put
an NSLog() in -resizeSubviewsWithOldSize: to check the
subview count, everything seems fine. I am completely
stumped.
2) If there are several columns of text and you select
all, then hit delete, the text is deleted but the
columns remain. This is a bug in TextEdit too - if you
delete all the text in the multiple pages view, all
the pages will remain even though there is no text,
until you start typing again and the pages get removed
so there is only one (as there should be).
If anybody has any suggestions as to how I can fix
these bugs, I would be very grateful.
All the best,
Keith
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
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