Thanks a lot to both of you for responding, and sorry for my delay!
(In case you forget the context, I have enclosed my original question below.)


>>   NSRect string_rect = CGRectIntegral( [mesg_string boundingRectWithSize: 
>> drawRect_.size

> What is drawRect_ set to when this is called?

Here is one case:
drawRect bounds = 0.000000 , 0.000000 , 2560.000000 , 1440.000000

After the call:
string rect bounds = 0.000000 , 0.000000 , 721.000000 , 44.000000

Actually, the bounds of drawRect_ are always the same (i.e., the size of the 
screen).

> With NSStringDrawingUsesLineFragmentOrigin, if the text is broken into lines, 
> it's not going to tell you the exact pixel width of the widest line.

I thought  CGRectIntegral() will take care of that, doesn't it?

> 
> In single-line mode (without NSStringDrawingUsesLineFragmentOrigin) I believe 
> you do get the exact dimensions of the single line of text.

In my case, mesg_string is (usually) two lines of text (one string with \n 
somewhere in the middle), 
but I tried it without NSStringDrawingUsesLineFragmentOrigin, to no avail.

>> 
>> 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.
> 
> I have had good results with CTLineGetTypographicBounds().

This seems to be suitable only for single lines of text.
But , usually, I've got 2 lines of text.

I looked at Core Text a bit further, but I could not find how to determine the 
width/height of a two-line string.



I have experimented a bit further, and tried this code:

    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];
    const CGSize textSize = [mesg sizeWithAttributes: attrsDictionary];         
     // mesg is a simple string, not attributed string
    textLayer_.bounds = NSMakeRect( 0, 0, textSize.width, textSize.height );
    textLayer_.shadowPath = CGPathCreateWithRect( textLayer_.bounds, NULL );    
  // set drop shadow

Again, the box of the shadow is too big, relative to the width of the text.
(But the height is OK.)


The excess width is sometimes more, sometimes a bit less, I could not find a 
pattern.
I guess , it has something to do with the actual glyphs in the string.



Best, G.


PS:
Here is my original question, just for reference


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

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

Reply via email to