• 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: Space Required By Text
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Space Required By Text


  • Subject: Re: Space Required By Text
  • From: Douglas Davidson <email@hidden>
  • Date: Wed, 30 May 2001 12:36:40 -0700

On Wednesday, May 30, 2001, at 09:51 AM, David Remahl wrote:

I have a text, which I would like to display in a text field. I would like to size the text field on run time to fit exactly the text. The text is line-broken by hard breaks, and occupies many lines, and approximately 80 columns. I would like to know the rect in which the text exactly fits using my font attributes.

Something like this:

NSTextStorage *storage = [[NSTextStorage alloc] initWithAttributedString:myAttrString];
NSLayoutManager *manager = [[NSLayoutManager alloc] init];
NSTextContainer *container = [[NSTextContainer alloc] init];
NSRect usedRect;

[storage addLayoutManager:manager];
[manager addTextContainer:container];
(void)[manager glyphRangeForTextContainer:container]; // cause layout
usedRect = [manager usedRectForTextContainer:container];

[container release];
[manager release];
[storage release];


This is just off the top of my head, so the details may not be exactly right, but this should give you an idea of how to proceed. The text container by default is very wide and very tall, so if you want to constrain layout in either width or height you should resize it; if you want lines broken only at the hard line breaks, leave it as is.

Note that there is a cost to creating all of these objects, so if you want to do multiple sizings you might want to keep them around instead of releasing them. Actually, the text storage will hold on to the other pieces, so you can release the layout manager and text container if you like. You can manipulate the text storage as you like if the string being sized changes, but remember that usedRectForTextContainer: doesn't cause relayout--you have to do something else, as in the above example, to force it.

Douglas Davidson


References: 
 >Space Required By Text (From: David Remahl <email@hidden>)

  • Prev by Date: Re: Space Required By Text
  • Next by Date: Re: Simple Port Traffic?
  • Previous by thread: Re: Space Required By Text
  • Next by thread: Re: Space Required By Text
  • Index(es):
    • Date
    • Thread