• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
bug in NSString sizeWithAttributes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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;
}


  • Prev by Date: Re: NSPopUpButton Problem
  • Next by Date: Re: On-the-fly text field formatting
  • Previous by thread: Re: NSPopUpButton Problem
  • Next by thread: Re: On-the-fly text field formatting
  • Index(es):
    • Date
    • Thread