NSAttributedString size method returning bogus data after adding NSParagraphStyle attribute
NSAttributedString size method returning bogus data after adding NSParagraphStyle attribute
- Subject: NSAttributedString size method returning bogus data after adding NSParagraphStyle attribute
- From: Sailor Quasar <email@hidden>
- Date: Fri, 13 Feb 2004 14:42:47 -0500
Sorry for the long subject line, but my understanding of list ettiquite
suggests it's better to have descriptive subjects on emails :)
Anyway, I have an NSMutableAttributedString that I'm using to layout
reasonably simple attributed text into an NSImage (NSCustomImageRep,
with a draw selector that calls [theAttributedString
drawAtPoint:NSMakePoint(0.0, 0.0)]. Naturally, in order to make this
work, I have to determine the size of the string's text before creating
(or drawing) the NSImage. I do this by generating the string like so
(yes, I know there's some loop inefficiencies):
NSParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle]
mutableCopy];
attributedString = [[NSMutableAttributedString alloc]
initWithString:@""];
for (unsigned i = 0; i < [textLines count]; ++i)
{
NSDictionary *lineData = [textLines objectAtIndex:i];
// alignment is an NSTextAlignment that was stuffed into an NSNumber
[style setAlignment:[[lineData objectForKey:@"align"] intValue];
[attributedString appendAttributedString:
[[[NSAttributedString alloc] initWithString:
[NSString stringWithFormat:"%@%@", (i ? @"\n" : @""), [lineData
objectForKey:@"text"]]
attributes:[NSDictionary dictionaryWithObjectsAndKeys:
[lineData objectForKey:@"font", NSFontAttributeName,
[[style copy] autorelease], NSParagraphStyleAttributeName,
[lineData objectForKey:@"color", NSForegroundColorAttributeName,
nil, nil]] autorelease]];
}
I then take the return value of [attributedString size].
This worked at one time. Unfortunately, different alignments stored in
the line data were coming out with identical alignments in the final
image. It turned out that I was passing "style" instead of "[style
copy]" for the NSParagraphStyle value, which of course wasn't working
since I was changing the alignment on the same object every time
through the loop. So I found and fixed that problem... Suddenly, the
NSImage no longer gets created at all.
Debugging very quickly revealed that the [attributedString size] call
was returning a ridiculous NSSize value. The height is exactly correct,
but the width is gigantic (4 million, 500 thousand, 100 pixels?). It
seems that having different NSParagraphStyles in the attributed string
is wrecking the text layout calculations, but I can't figure out why or
any way around it without rewriting an entire set of otherwise working
code to use a more complicated text layout method that I really
shouldn't need. Am I doing something wrong, or have I actually found a
bug in NSAttributedString?
-- Sailor Quasar, guardian of Leraz's memory
"A face of stone may hide a soul with the deepest Love of all"
Email: email@hidden
_______________________________________________
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.