• 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
NSAttributedString boundingRectWithSize vs. paragraph margins
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

NSAttributedString boundingRectWithSize vs. paragraph margins


  • Subject: NSAttributedString boundingRectWithSize vs. paragraph margins
  • From: Matt Neuburg <email@hidden>
  • Date: Wed, 23 Jan 2013 18:29:01 -0800

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.


--
matt neuburg, phd = email@hidden, http://www.apeth.net/matt/
pantes anthropoi tou eidenai oregontai phusei
Programming iOS 5! http://shop.oreilly.com/product/0636920023562.do
RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
TidBITS, Mac news and reviews since 1990, http://www.tidbits.com


_______________________________________________

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: NSAttributedString boundingRectWithSize vs. paragraph margins
      • From: Keary Suska <email@hidden>
  • Prev by Date: Re: Coordinate conversions in CALayer
  • Next by Date: can update the UI from sheetDidEnd:…. ?
  • Previous by thread: Re: New iTunes UI
  • Next by thread: Re: NSAttributedString boundingRectWithSize vs. paragraph margins
  • Index(es):
    • Date
    • Thread