Problems with NSLayoutManager and NSTextContainer
Problems with NSLayoutManager and NSTextContainer
- Subject: Problems with NSLayoutManager and NSTextContainer
- From: Donald Brown <email@hidden>
- Date: Tue, 21 Jan 2003 13:11:08 -0600
I've got a weird problem when I'm using NSLayoutManager with multiple
NSTextContainers to break up a long NSString or NSAttributedString into
columns. One line seems lost at the junction. Here's the code I've
got, I know it's long, but it's a complex routine. Does anyone have
any ideas?
Donald
------
//complicated printing
NSTextStorage* textStorage;
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
int iHeight = rRawRect.size.height;
int iContainerIndex;
NSTextContainer* textContainer;
NSMutableArray* containerArray = [NSMutableArray
arrayWithCapacity:2];
NSMutableArray* xArray = [NSMutableArray arrayWithCapacity:2];
NSMutableArray* yArray = [NSMutableArray arrayWithCapacity:2];
if ([sData isKindOfClass:[NSAttributedString class]])
{
NSAttributedString* aData = (NSAttributedString*)sData;
textStorage = [[NSTextStorage alloc] initWithString:@""];
[textStorage setAttributedString:aData];
}
else
{
textStorage = [[NSTextStorage alloc] initWithString:sData];
[textStorage addAttributes:style range:NSMakeRange(0,
[textStorage length])];
}
[textStorage addLayoutManager:layoutManager];
while (iHeight>0)
{
NSRect colRect = [self rectForPage:iPage column:iColumn];
int iHeight2 = drawHeight - iTop;
NSRect itemRect;
iHeight += 10;
if (iHeight < iHeight2)
iHeight2 = iHeight;
itemRect = NSMakeRect(colRect.origin.x + rRawRect.origin.x,
NSMaxY(colRect) - iTop - iHeight2,
rRawRect.size.width, iHeight2);
textContainer = [[NSTextContainer alloc]
initWithContainerSize:NSMakeSize(rRawRect.size.width,
iHeight2)];
[textContainer setLineFragmentPadding:0.0];
[textContainer setHeightTracksTextView:NO];
[layoutManager addTextContainer:textContainer];
[containerArray addObject:textContainer];
[xArray addObject:[NSNumber numberWithFloat:(colRect.origin.x +
rRawRect.origin.x)]];
[yArray addObject:[NSNumber numberWithFloat:(NSMaxY(colRect) -
iTop - iHeight2)]];
[textContainer release]; //Now owned by the layoutManager
iHeight = iHeight - iHeight2;
iTop = 0;
iColumn++;
if (iColumn==numColumns)
{
iPage++;
iColumn = 0;
}
}
for (iContainerIndex = 0; iContainerIndex < [containerArray
count]; iContainerIndex++)
{
textContainer = [containerArray objectAtIndex:iContainerIndex];
NSNumber* xNumber = [xArray objectAtIndex:iContainerIndex];
NSNumber* yNumber = [yArray objectAtIndex:iContainerIndex];
NSRange rRange = [layoutManager
glyphRangeForTextContainer:textContainer];//calculate this bit
NSRange rRange2 = [layoutManager
characterRangeForGlyphRange:rRange actualGlyphRange:NULL];
NSAttributedString* aS2 = [textStorage
attributedSubstringFromRange:rRange2];
NSSize tcSize = [textContainer containerSize];
NSRect itemRect = NSMakeRect([xNumber floatValue], [yNumber
floatValue], tcSize.width, tcSize.height);
[aS2 drawInRect:itemRect];
NSFrameRect(itemRect);
}
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.