Problem:
--------

I'm seeing incorrect results when I try to get the height of a string with a particular font and maximum width.


Context:
--------

I'm running the code below. I have a single NSWindow with a checkbox and a button. In the awakeFromNib method of the main controller, I change the attribute of the checkbox string. In the compute: method, I compute the height requested to display a string for a specific font and maximum width.

If I run this code as is, I get 17.0 as the required height. That's incorrect.

If I comment the awakeFromNib code, I get 34.0f as the required height. That's correct.


Questions:
---------

Is it a bug in NSLayoutManager?

If so, is there a workaround?

If it's not, what am I missing?

-----8<---------8<--------8<--------8<--------8<--------8<--------8<---- ----

#import "MainController.h"


@implementation MainController

- (void) awakeFromNib
{
        NSAttributedString * tAttributedString;
        
tAttributedString=[[NSAttributedString alloc] initWithString: [IBcheckBox_ title] attributes:[NSDictionary dictionaryWithObjectsAndKeys:[IBcheckBox_ font],NSFontAttributeName, [NSNumber numberWithInt:NSUnderlineStyleSingle],NSStrikethroughStyleAttributeName, nil]];

        if (tAttributedString!=nil)
        {
                [IBcheckBox_ setAttributedTitle:tAttributedString];
                
                [tAttributedString release];
        }
}

- (IBAction) compute:(id) sender
{
        float tHeight=0;
        NSString * tString=@"It's a long list title, don't you think?";
        float tMaxWidth=134.0f;
        NSFont * tFont;
        
        tFont=[NSFont systemFontOfSize:13.0f];
        
        if (tString!=nil)
        {
                NSTextStorage * tTextStorage;
                        
                tTextStorage=[[NSTextStorage alloc] initWithString:tString];
                
                if (tTextStorage!=nil)
                {
                        NSTextContainer * tTextContainer;
                        
tTextContainer = [[NSTextContainer alloc] initWithContainerSize: NSMakeSize(tMaxWidth, FLT_MAX)];
                        
                        if (tTextContainer!=nil)
                        {
                                NSLayoutManager * tLayoutManager;
                                
                                tLayoutManager = [[NSLayoutManager alloc] init];
                                
                                if (tLayoutManager!=nil)
                                {
[tLayoutManager setTypesetterBehavior:NSTypesetterBehavior_10_2_WithCompatibility];
                                        
                                        [tLayoutManager 
addTextContainer:tTextContainer];
                                        
                                        [tTextStorage 
addLayoutManager:tLayoutManager];

[tTextStorage addAttribute:NSFontAttributeName value:tFont range:NSMakeRange(0, [tTextStorage length])];
                                        
                                        [tTextContainer 
setLineFragmentPadding:0.0f];
                                
                                        [tLayoutManager 
glyphRangeForTextContainer:tTextContainer];
                                        
tHeight=NSHeight([tLayoutManager usedRectForTextContainer:tTextContainer]);
                                        
                                        [tLayoutManager release];
                                }
                                
                                [tTextContainer release];
                        }
                        
                        [tTextStorage release];
                }
        }
        
        NSLog(@"%@: %f %f %@",tString,tHeight,tMaxWidth,tFont);
}

@end
_______________________________________________

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 arch...@mail-archive.com

Reply via email to