• 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
boundingRectWithSize gives wrong size
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

boundingRectWithSize gives wrong size


  • Subject: boundingRectWithSize gives wrong size
  • From: Gabriel Zachmann via Cocoa-dev <email@hidden>
  • Date: Wed, 28 Aug 2019 21:33:23 +0200

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
wide.
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
problem.
(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]
init];

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

attributes: attrsDictionary];

   NSRect string_rect = CGRectIntegral( [mesg_string boundingRectWithSize:
drawRect_.size
                                               options:
NSStringDrawingUsesLineFragmentOrigin |
                                                         //
NSStringDrawingUsesDeviceMetrics |       // tried with and w/o


NSStringDrawingUsesFontLeading
                                               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 (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: boundingRectWithSize gives wrong size
      • From: Jens Alfke via Cocoa-dev <email@hidden>
    • Re: boundingRectWithSize gives wrong size
      • From: Richard Charles via Cocoa-dev <email@hidden>
  • Prev by Date: Re: Recommendations for cross platform library/framework
  • Next by Date: Re: boundingRectWithSize gives wrong size
  • Previous by thread: Re: Recommendations for cross platform library/framework
  • Next by thread: Re: boundingRectWithSize gives wrong size
  • Index(es):
    • Date
    • Thread