What happened to [NSString sizeWithAttributes:]?
What happened to [NSString sizeWithAttributes:]?
- Subject: What happened to [NSString sizeWithAttributes:]?
- From: Perry Clarke <email@hidden>
- Date: Sun, 25 Jul 2004 14:12:37 -0700
I wonder if anyone can shed some light on this ...
Some time in the recent past (perhaps when 10.3.4 was released) a
bizarre and undocumented limit was introduced into [NSString
sizeWithAttributes] - it now fails for strings containing more than
exactly 2000 characters (a suspiciously round number :-)
This code:
uint numChars = 0;
NSSize extent;
for (numChars = 1998; numChars < 2003; numChars += 1)
{
char *testData = calloc(numChars+1, sizeof(char));
memset(testData, 'a', numChars);
NSString *testStr = [NSString stringWithCString:testData];
extent = [testStr sizeWithAttributes:nil];
NSLog(@"testStr: %d characters - sizeWithAttributes: %.1f, %.1f",
[testStr length], extent.width, extent.height);
free(testData);
}
produces this output:
testStr: 1998 characters - sizeWithAttributes: 13986.0, 15.0
testStr: 1999 characters - sizeWithAttributes: 13993.0, 15.0
testStr: 2000 characters - sizeWithAttributes: 14000.0, 15.0
testStr: 2001 characters - sizeWithAttributes: 0.0, 0.0
testStr: 2002 characters - sizeWithAttributes: 0.0, 0.0
It doesn't matter what letter other than 'a' is used in the test case
(if 'i' is used, for example, the width of a 2000 character string is a
lot less but the failure still occurs at 2001 characters). And,
obviously, the nil attributes are just for the test case; the same
problem occurs with a real set of attributes.
The failure is signaled by a return of a zero size rather than an
exception.
I believe the bug has various other side-effects too. For instance,
[NSString drawAtPoint: withAttributes:] no longer seems to work with
strings longer than 2000 characters either (though [NSString
drawInRect: withAttributes:] does).
The regression has the effect that code that works on Jaguar no longer
works on Panther (though I'm sure it did until a recent system update).
I could find nothing in any release notes about this.
You may be wondering why I care about measuring the size of >2000
character strings. It's to implement a "ticker" (or "marquee") style
display. There are other limits that I have to care about as well such
as the maximum allowed dimensions of an image that [NSView lockFocus]
will work on.
Perry
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
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.