NSTextField sizing to fit
NSTextField sizing to fit
- Subject: NSTextField sizing to fit
- From: Nicholas Riley <email@hidden>
- Date: Tue, 17 Dec 2002 09:42:32 -0600
- Mail-followup-to: email@hidden
Hi,
I'm trying to make a NSTextField fit to the height of its containing
text given a particular width. I'm getting a height back from Cocoa
which is almost what i want, but a few pixels short. Any idea where
I can get a more accurate number? Here's what I've tried so far:
@implementation NSAttributedString (NJRExtensions)
- (float)heightWrappedToWidth:(float)width;
{
float height = 0;
NSTextStorage *storage = [[NSTextStorage alloc] initWithAttributedString: self];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *container = [[NSTextContainer alloc]
initWithContainerSize: NSMakeSize(width, MAXFLOAT)];
[layoutManager addTextContainer: container];
[storage addLayoutManager: layoutManager];
(void) [layoutManager glyphRangeForTextContainer: container]; //force layout
height = [layoutManager usedRectForTextContainer: container].size.height;
[layoutManager release];
[container release];
[storage release];
return height;
}
@end
I'm pretty sure that code is correct - I wrote it myself but just did
a mailing list search and found Douglas Davidson's from a year ago
which was almost identical.
Then I go on to use that height as an argument to setFrame:, but the
descenders of the bottommost line get cut off (approximately).
I tried this to compensate:
NSTextView *tv = (NSTextView *)[window fieldEditor: YES
forObject: alertView];
NSSize textInset = [tv textContainerInset];
NSLog(@"inset is %@", NSStringFromSize(textInset));
NSLog(@"origin is %@", NSStringFromPoint([tv textContainerOrigin]));
but I just get back (0,0) for both of them. I guess there's some
additional padding being inserted somewhere, but I can't find it.
Thanks,
--
=Nicholas Riley <email@hidden> | <
http://www.uiuc.edu/ph/www/njriley>
Pablo Research Group, Department of Computer Science and
Medical Scholars Program, University of Illinois at Urbana-Champaign
_______________________________________________
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.