NSLayoutManager looping with custom NSTextContainer
NSLayoutManager looping with custom NSTextContainer
- Subject: NSLayoutManager looping with custom NSTextContainer
- From: Sergei Borodavkin <email@hidden>
- Date: Fri, 27 May 2005 18:35:46 +0300
In my project I've overridden NSTextContainer to provide text wrapping around miscellaneous vector shapes (such as stars, circles, rectangles, etc). As all you know, to implement this we have to write our own lineFragmentRectForProposedRect method, which wraps the text as needed.
Everything works ok, but sometimes the text system gets into some kind of an infinite loop, and it calls my lineFragmentRectForProposedRect, passing to it proposedRect
, which has _the same_ origin.y at each call.
So, the key symptoms of this situation are:
1. proposedRect.origin.y has the same value at the beginning of each lineFragmentRectForProposedRect call (hundreds, thousands, millions of time). It is often 0, but I saw also 74.
2. proposedRect.size.height vary from call to call between some two values (well, I haven't seen more). I suppose that these values are heights of two consecutive line rects situated at the place where looping happens.
Let me call this situation of infinite looping as "LOOP".
The possible solution was to add some static variables to this method (like prevOriginYFromLM, prevHeightFromLM), and assign appropriate values to them at each call, so I could check if (prevOriginYFromLM == proposedRect.origin.y) for current proposedRect, and, if it's true, just shift the proposedRect down by the value of prevHeightFromLM.
Unfortunately, it is not so simple as it looks. When text alignment is "justified", the algorithm of layout works with returns (e.g. the word is too short, so layout manager should come back, increase the kerning, and send the same rect to my function again). It means that even if prevOriginYFromLM == proposedRect.origin.y, it doesn't mean LOOP, but, according to my solution, I treat this as LOOP, shift the proposedRect, and since this moment everything goes wrong. For example, if text lines have different heights, this solution can easily include additional unneeded spacing between them. So, at one side we have infinite looping (of NSLayoutManager, I suppose), and on the other - empty space between lines with different heights (e.g. font sizes).
I will be appreciated for any advices.
Respectfully,
Sergei Borodavkin.
_______________________________________________
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