I am trying to determine the size of a piece of text using boundingRectWithSize.

The problem is that this method does not return the correct width.
(The height seems to be about right.)
Sometimes, the width is only a little bit too wide, sometimes it is much too 
And I could not find a pattern, it is not just a linear factor.

Below is the code snippet that I use.

I already did considerable googling but could not find a solution for my 
(Most posts say that I needed to use the correct options, so I included them, 
to no avail.)

I would appreciate very much all kinds of pointers or suggestions.

Best regards, Gabriel

   NSMutableString * mesg = NULL;
   textLayer_.string = mesg;
   textLayer_.fontSize = fontSize_;

   NSStringDrawingContext * ctxt = [NSStringDrawingContext new];    
   ctxt.minimumScaleFactor = 1.0;
   NSFont * font = [NSFont fontWithName: @"Andale Mono" size: 
textLayer_.fontSize ];
   NSMutableParagraphStyle * paragraphStyle = [[NSMutableParagraphStyle alloc] 

   paragraphStyle.lineBreakMode = NSLineBreakByClipping;
   NSDictionary * attrsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                     font, NSFontAttributeName,
NSParagraphStyleAttributeName, nil];
   NSAttributedString * mesg_string = [[NSAttributedString alloc] 
initWithString: mesg
attributes: attrsDictionary];

   NSRect string_rect = CGRectIntegral( [mesg_string boundingRectWithSize: 
NSStringDrawingUsesLineFragmentOrigin |
NSStringDrawingUsesDeviceMetrics |       // tried with and w/o                  
                                               context: ctxt ] );

   string_rect.size.height += 0.2 * textLayer_.fontSize;    // for the shadow
   if ( string_rect.size.width < drawRect_.size.width )
       string_rect.size.width *= 0.9;                                    //  
   textLayer_.bounds = string_rect;

   // set drop shadow
   textLayer_.shadowPath = CGPathCreateWithRect( string_rect, NULL );

   [textLayer_ setNeedsDisplay];

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


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:

This email sent to arch...@mail-archive.com

Reply via email to