NSLayoutManager and avoiding widows and orphans
NSLayoutManager and avoiding widows and orphans
- Subject: NSLayoutManager and avoiding widows and orphans
- From: Keith Blount <email@hidden>
- Date: Tue, 6 Nov 2007 16:39:00 -0800 (PST)
Hi,
My application needs to avoid widows and orphans when printing. It has code that does this, but the code crashes in certain circumstances, and I am now on my third cycle of development in which I'm having to try to address this bug when I thought I'd fixed it, so I'm hoping someone here can give me some advice.
Basically, my code just needs to check the line at the bottom of any given text container and check that it's not the last line in the paragraph or the first line in the paragraph. If it is, with a couple of other minor adjustments, it adjusts the height of the text container and text view that contain that text so that it is shorter and pushes that paragraph over into the next text container.
All this is fine - I have the code up and running to do that. The problem is that NSLayoutManager is very tricky. Do things at the wrong time during layout and it crashes. So the trouble is, where to do this code?
At first, I did it all in the -didCompleteLayout method. This was a disaster, and caused a lot of crashes.
So then, I tried the following:
- During layout, in the -didCompleteLayout method, look for a widow and orphan. If one was noticed, just save the information about it - which container it was in, and how much we would need to adjust the container by.
- At the end of layout, adjust the frame of the text view and container that was affected.
- Once this was done, force layout again, so that the next widow or orphan was found.
- Repeat until all were found.
This seemed to work fine... But it doesn't. It turns out that if enough widows and orphans are found in a long document, so that another page (and thus another container) gets added to the end of the document, the adding of this page causes havoc and suddenly I'm caught in an infinite loop and a hang.
So, next I tried waiting until all layout is complete and then enumerating through the text containers, checking for a widow or orphan at the end of each one, adjusting the frame and then forcing layout. But again, if a page gets added because of this, we hit problems - bounds and run storage exceptions, and problems caused by adding a text container whilst enumerating through the text containers.
All in all, not good.
Does anyone have any suggestions of the safest place in which to, and how best I could, go through all my text containers and adjust their sizes when necessary? Like I say, it's not the widows and orphans code I need, that bit I have. It's the best place in which actually to resize textviews/containers after layout, one by one, as necessary, without causing crashes and hangs because I've upset the layout manager...
Thanks in advance to anyone who can help.
All the best,
Keith
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
_______________________________________________
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