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

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.

  • Follow-Ups:
    • Re: NSTextField sizing to fit
      • From: Mike Ferris <email@hidden>
  • Prev by Date: Re: AddressBook: Why can't I add a person, that is not (yet) in the AB, to a group?
  • Next by Date: Re: NSTableView - Alternating blue and white background
  • Previous by thread: Re: Need some help with SmallSockets/Networking
  • Next by thread: Re: NSTextField sizing to fit
  • Index(es):
    • Date
    • Thread