layoutManager:didCompleteLayoutForTextContainer:atEnd: and multi-column problem
layoutManager:didCompleteLayoutForTextContainer:atEnd: and multi-column problem
- Subject: layoutManager:didCompleteLayoutForTextContainer:atEnd: and multi-column problem
- From: Keith Blount <email@hidden>
- Date: Sun, 2 Apr 2006 05:28:38 -0700 (PDT)
Hello,
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 arranged side-by-side, which can
be resized vertically 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.
However, I have two problems, one of which is spewing
an exception, 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: when
removing columns, if a resize is done really quickly
after another one. 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.
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