Is there any equivalent method in AppKit (for Cocoa on Mac OS X) that does the same as UIKit [NSString sizeWithFont:constrainedToSize:] ?
If not, how can I get the amount of space needed to render a specific row limited by width / height?
Update: The following is a snippet of the code I'm using that I expect will produce the results I got after.
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys: [NSFont systemFontOfSize: [NSFont smallSystemFontSize]], NSFontAttributeName, [NSParagraphStyle defaultParagraphStyle], NSParagraphStyleAttributeName, nil]; NSSize size = NSMakeSize(200.0, MAXFLOAT); NSRect bounds; bounds = [@"This is a really really really really really really really long string that won't fit on one line" boundingRectWithSize: size options: NSStringDrawingUsesFontLeading attributes: attributes]; NSLog(@"height: %02f, width: %02f", bounds.size.height, bounds.size.width);
I would expect the width of the output file to be 200 and the height to be greater than the height of one line, however it produces:
height: 14.000000, width: 466.619141
Thanks!
cocoa nsstring appkit
Bryan kyle
source share