Re: Getting bounding rect of NSAttributedString
Re: Getting bounding rect of NSAttributedString
- Subject: Re: Getting bounding rect of NSAttributedString
- From: Ricky Sharp <email@hidden>
- Date: Tue, 25 Jul 2006 06:20:32 -0500
On Monday, July 24, 2006, at 10:45PM, Simone Manganelli <email@hidden> wrote:
>Does anybody know a viable workaround to the continuing problem of
>NSAttributedStrings not returning the correct bounding rect? The
>boundingRectwithSize:options:attributes method doesn't seem to
>respect the attributes that you pass to it, specifically the
>NSParagraphStyle telling the text to wrap on words. When actually
>drawing the NSAttributedString, the text is correctly word-wrapped,
>but I still need the bounding rectangle so that I can center the text
>inside an image. For example, the code:
>
>> NSMutableParagraphStyle *paragStyle = [[NSMutableParagraphStyle
>> alloc] init];
>> [paragStyle setParagraphStyle:[NSParagraphStyle
>> defaultParagraphStyle]];
>> [paragStyle setLineBreakMode:NSLineBreakByWordWrapping];
>> NSDictionary *attributesDict = [[NSDictionary alloc]
>> initWithObjectsAndKeys:[NSFont fontWithName:@"Helvetica Neue
>> Condensed Bold" size:12.0],NSFontAttributeName,
>> paragStyle,NSParagraphStyleAttributeName,
>> nil];
>> NSAttributedString *curSquareText = [[NSAttributedString alloc]
>> initWithString:[curEntry bingoEntryText]
>> attributes:attributesDict];
>> NSRect boundsRect = [[curEntry bingoEntryText]
>> boundingRectWithSize:NSMakeSize(squareWidth,squareHeight)
>> options:nil attributes:attributesDict];
>> NSLog(@"max: ò x ò, size: ò x ò",squareWidth, squareHeight,
>> boundsRect.size.width, boundsRect.size.height);
>
>produces
>
>> max: 98.0000002 x 80.0000002, size: 260.0000002 x 19.0000002
>
>which means that the text hasn't been wrapping on words, as it was
>specified in the NSParagraphStyle.
>
>I found what was supposed to be a workaround here on the Apple
>mailing lists ( http://lists.apple.com/archives/cocoa-dev/2006/Apr/
>msg01532.html ), but the workaround does not seem to work for me. It
>just returns the same unwrapped size that's outside the bounds of the
>specified NSSize.
>
>Any suggestions or other workarounds?
You may want to have a look at the TipWrapper sample code:
<http://developer.apple.com/samplecode/TipWrapper/index.html>
I used that code as a starting point to implement custom help tags (overlay windows containing a view which would render an NSAttributedString.
Basically, the TipWrapper code was used to measure wrapped strings. I took the width/height of the NSTextView that it uses and used that for the attributed string's bounds.
Note that it's important that you set the attributes identically between the string and text view. I've only used it with a bold system font of size 24.0, but have never run into any problems where the measured values didn't provide the proper bounds for the string.
--
Rick Sharp
Instant Interactive(tm)
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden