• 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: [NSTextStorage/NSAttributedString] How can we know the height of a rendered string?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [NSTextStorage/NSAttributedString] How can we know the height of a rendered string?


  • Subject: Re: [NSTextStorage/NSAttributedString] How can we know the height of a rendered string?
  • From: Jean-Daniel Dupas <email@hidden>
  • Date: Sun, 6 Jul 2008 23:26:06 +0200


Le 6 juil. 08 à 21:25, Stéphane Sudre a écrit :

Problem:
--------

I would like/need to know the height that would be required to render a string inside a fixed width box.

Solution that does not work:
----------------------------

So far, I've been using a solution found from a google search and which looks like this:

- (float) heightOfString:(NSString *) inString forFont:(NSFont *) inFont andMaxWidth:(float) inMaxWidth
{
float tHeight=0;

if (inString!=nil)
{
NSTextStorage * tTextStorage;

tTextStorage=[[NSTextStorage alloc] initWithString:inString];

if (tTextStorage!=nil)
{
NSTextContainer * tTextContainer;

tTextContainer = [[NSTextContainer alloc] initWithContainerSize: NSMakeSize(inMaxWidth, FLT_MAX)];

if (tTextContainer!=nil)
{
NSLayoutManager * tLayoutManager = [[NSLayoutManager alloc] init];

tLayoutManager = [[NSLayoutManager alloc] init];

if (tLayoutManager!=nil)
{
[tLayoutManager addTextContainer:tTextContainer];

[tTextStorage addLayoutManager:tLayoutManager];


[tTextStorage addAttribute:NSFontAttributeName value:inFont range:NSMakeRange(0, [tTextStorage length])];

[tTextContainer setLineFragmentPadding:0.0];


[tLayoutManager glyphRangeForTextContainer:tTextContainer];


tHeight=NSHeight([tLayoutManager usedRectForTextContainer:tTextContainer]);

[tLayoutManager release];
}

[tTextContainer release];
}

[tTextStorage release];
}
}

return tHeight;
}

usedRectForTextContainer: causes neither glyph generation nor layout, and so the result is may be inaccurate (the layout manager may be computing the layout in background and does not have finish).
You have to make sure it is ready before using this function.


[tLayoutManager ensureLayoutForTextContainer:tTextContainer];






Attachment: smime.p7s
Description: S/MIME cryptographic signature

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: [NSTextStorage/NSAttributedString] How can we know the height of a rendered string?
      • From: Stéphane Sudre <email@hidden>
References: 
 >[NSTextStorage/NSAttributedString] How can we know the height of a rendered string? (From: Stéphane Sudre <email@hidden>)

  • Prev by Date: [NSTextStorage/NSAttributedString] How can we know the height of a rendered string?
  • Next by Date: Re: [Solved] Using key-value observing on NSUserDefaults
  • Previous by thread: [NSTextStorage/NSAttributedString] How can we know the height of a rendered string?
  • Next by thread: Re: [NSTextStorage/NSAttributedString] How can we know the height of a rendered string?
  • Index(es):
    • Date
    • Thread