Re: NSAttributedString boundingRectWithSize vs. paragraph margins
Re: NSAttributedString boundingRectWithSize vs. paragraph margins
- Subject: Re: NSAttributedString boundingRectWithSize vs. paragraph margins
- From: Matt Neuburg <email@hidden>
- Date: Wed, 23 Jan 2013 20:17:34 -0800
I should have been explicit that this is iOS. Sorry.
It may be that the height is right despite the narrowed width, but since the narrowed width is unexpected, it would be nice to be assured of this. m.
-----
Sent from my iPad, but I am not a dork. Really.
On Jan 23, 2013, at 7:18 PM, Keary Suska <email@hidden> wrote:
> 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 (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