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;
}


The issue with this solution is that it does not return a correct result. 1 or 2 lines are OK (or can be corrected). Over 3 lines, the results are incorrect (or can not be corrected reliably).

Question:
---------

Which solution would work?



_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to