On Jan 23, 2013, at 7:29 PM, Matt Neuburg wrote:

> The docs on boundingRectWithSize:options:context: say:
> 
>  "Typically, the renderer preserves the width constraint and adjusts the 
> height constraint as needed."
> 
> Yes, that's what I was hoping for. But in fact I'm getting a much narrower 
> width than what I supplied.
> 
> I suspect that the problem here has to do with differing expectations as to 
> what "bounding rect" means, and as to what my paragraph margins mean. Here's 
> some more complete code:
> 
>    NSString* sentence = @"This is a test. ";
>    NSString* s = @"";
>    for (int i = 0; i < 20; i++)
>        s = [s stringByAppendingString:sentence];
>    NSMutableParagraphStyle* mps = [NSMutableParagraphStyle new];
>    mps.headIndent = 20;
>    mps.firstLineHeadIndent = 20;
>    mps.tailIndent = -20;
>    NSAttributedString* as = [[NSAttributedString alloc] initWithString:s 
> attributes:@{
>                                          NSParagraphStyleAttributeName:mps
>                              }];
>    CGRect r = [as boundingRectWithSize:CGSizeMake(100,10000) 
>        options:NSStringDrawingUsesLineFragmentOrigin context:nil];
>    NSLog(@"%@", NSStringFromCGRect(r));
> 
> The resulting width is 51.8. So it looks as if that's the width *after* the 
> margins (20 and 20) have squeezed the text inward. What we're getting, it 
> seems, is the minimum bounding rect containing any text. But that's not what 
> I want to know; I want to know the height of this text as drawn within the 
> width I supplied (100) using the paragraph margins I supplied. Is there some 
> other way to find that out? Or is this a bug with regard to how margins are 
> interpreted? m.

What height value are you getting that you believe is incorrect? If the value 
is unchanged (1000), it could mean that the layout manager is constructing an 
NSTextContainer with your desired size (100,10000) and fitting text within it. 
My experience has been that NSLayoutManager will not shrink containers, but 
will grow them. If you set the height to a small value, do you get a different 
and more expected value?

I didn't know about this method--I had to construct an NSTextView as laid out 
in the "Text System User Interface Layer Programming Guide" under "Setting Up 
the Text View", set the attributed string, then call -[NSLayoutManager 
getGlyphs:range:] or similar to force rendering. I believe this approach will 
do exactly what you want if all else fails...

HTH,

Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"


_______________________________________________

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