bug in NSString sizeWithAttributes
bug in NSString sizeWithAttributes
- Subject: bug in NSString sizeWithAttributes
- From: Tom Waters <email@hidden>
- Date: Sat, 14 Jul 2001 15:46:53 -0700
Does it seem reasonable to expect that NSString sizeWithAttributes:
would return the same width if the paragraph style was center justified
vs. left justified?
I am finding that if I ask for the size of a string while anything but
NSLeftTextAlignment is set in the attributes, I get back a truncated
width. If you then try to tell that string to drawInRect a rectangle
sized based on this information, the text will wrap, leaving off the
last character.
Before I file a bug with apple, can someone confirm that this is indeed
anomalous behavior?
Here is the output of the code below:
Jul 14 15:43:16 TextSizeBug[2622] left aligned width = 40.136719
Jul 14 15:43:16 TextSizeBug[2622] center aligned width = 40.000000
Jul 14 15:43:16 TextSizeBug[2622] right aligned width = 40.000000
#import <Cocoa/Cocoa.h>
int main(int argc, const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSMutableDictionary *attr;
NSFont *font = [NSFont labelFontOfSize: 12];
NSMutableParagraphStyle *para = [[[NSMutableParagraphStyle alloc]
init] autorelease];
NSString *str = @"Library";
NSSize size;
attr = [NSMutableDictionary dictionaryWithObjectsAndKeys:
font, NSFontAttributeName,
[NSColor blackColor], NSForegroundColorAttributeName,
para, NSParagraphStyleAttributeName,
nil];
[para setAlignment: NSLeftTextAlignment];
size = [str sizeWithAttributes: attr];
NSLog(@"left aligned width = %f", size.width);
[para setAlignment: NSCenterTextAlignment];
size = [str sizeWithAttributes: attr];
NSLog(@"center aligned width = %f", size.width);
[para setAlignment: NSRightTextAlignment];
size = [str sizeWithAttributes: attr];
NSLog(@"right aligned width = %f", size.width);
[pool release];
return 0;
}