• 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
Re: NSTextField sizing to fit
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTextField sizing to fit


  • Subject: Re: NSTextField sizing to fit
  • From: Mike Ferris <email@hidden>
  • Date: Tue, 17 Dec 2002 08:53:42 -0800

How about using NSTextFieldCell's -cellSizeForBounds:? Give it a rect with the width you want and a big height. Since this is NSTextFieldCell, the size it returns will be appropriate. If you use the text system directly as your sample code did, you'll get something that fits the text but not necessarily any extra padding or borders or whatever that NSTextFieldCell uses.

Mike


Begin forwarded message:

From: Nicholas Riley <email@hidden>
Date: Tue Dec 17, 2002 7:42:32 AM US/Pacific
To: email@hidden
Subject: NSTextField sizing to fit

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.
_______________________________________________
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.

  • Follow-Ups:
    • Re: NSTextField sizing to fit
      • From: Nicholas Riley <email@hidden>
References: 
 >NSTextField sizing to fit (From: Nicholas Riley <email@hidden>)

  • Prev by Date: Re: Breaking
  • Next by Date: Methods with Variable Numbers of Arguments--Second Attempt
  • Previous by thread: NSTextField sizing to fit
  • Next by thread: Re: NSTextField sizing to fit
  • Index(es):
    • Date
    • Thread