Re: Trouble finding bounding rect of NSAttributedString

2009-11-20 Thread Jens Alfke

On Nov 19, 2009, at 9:20 PM, Dave DeLong wrote:

 However, in both of these methods, the attributedString of the textview is 
 nil, thus giving me the strange results I'm seeing.

How are you getting the attributedString out of the textview? (I'm guessing by 
calling -textStorage.) If so, that should never return nil — make sure the 
NSTextView* itself isn't nil :)

—Jens___

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

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


Trouble finding bounding rect of NSAttributedString

2009-11-19 Thread Dave DeLong
Hi everyone,

I've been struggling for several hours to get the bounding rectangle of an 
NSAttributedString when drawn at a particular width.

I have an NSTextView that I need to resize dynamically.  NSTextView appears to 
resize itself as I add text, but I also need it to shrink itself as I delete 
text.  Here's what I've tried:

[myAttributedString size]
[myAttributedString boundingRectWithSize:NSMakeSize(desiredWidth, FLT_MAX) 
options:NSStringDrawingUsesDeviceMetrics];
[[myTextView layoutManager] usedRectForTextContainer:[myTextView textContainer]]
[[myTextView layoutManager] boundingRectForGlyphRange:NSMakeRange(0, 
[myAttributedString length]) inTextContainer:[myTextView textContainer]]
... and a few others

I've been over the documentation for NSAttributedString, NSLayoutManager, and 
NSTextContainer, and I've tried just about everything that looks like it might 
give me what I'm looking for.  However, I've obviously missed something pretty 
fundamental to getting my string's rect.  Any pointers?

Thanks!

Dave DeLong

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

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

Re: Trouble finding bounding rect of NSAttributedString

2009-11-19 Thread Jens Alfke

On Nov 19, 2009, at 1:28 PM, Dave DeLong wrote:

 I have an NSTextView that I need to resize dynamically.  NSTextView appears 
 to resize itself as I add text, but I also need it to shrink itself as I 
 delete text.  Here's what I've tried:

Several of those look like reasonable calls for this purpose. You didn't 
specify what went wrong when you called them ... ?

—Jens___

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

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


Re: Trouble finding bounding rect of NSAttributedString

2009-11-19 Thread Dave DeLong
Good point.

When I print out the size or rect that these return, I invariably get either 
{0, 0} or something absurd like {{1.17076e-318, 2.29357e-314}, {2.30359e-314, 
2.1224e-314}} (that's just running the rect through NSStringFromRect())

Dave

On Nov 19, 2009, at 3:37 PM, Jens Alfke wrote:

 
 On Nov 19, 2009, at 1:28 PM, Dave DeLong wrote:
 
 I have an NSTextView that I need to resize dynamically.  NSTextView appears 
 to resize itself as I add text, but I also need it to shrink itself as I 
 delete text.  Here's what I've tried:
 
 Several of those look like reasonable calls for this purpose. You didn't 
 specify what went wrong when you called them ... ?
 
 —Jens



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

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

Re: Trouble finding bounding rect of NSAttributedString

2009-11-19 Thread Jens Alfke

On Nov 19, 2009, at 2:39 PM, Dave DeLong wrote:

 When I print out the size or rect that these return, I invariably get either 
 {0, 0} or something absurd like {{1.17076e-318, 2.29357e-314}, {2.30359e-314, 
 2.1224e-314}} (that's just running the rect through NSStringFromRect())

Make sure the receiver (the NSAttributedString) isn't nil. The Obj-C runtime 
gives undefined results for a struct-based method return value if the receiver 
is nil; so you'll end up with garbage like this. (This is because it doesn't 
know at runtime how large the struct is, so it can't safely fill it in with 
zeroes.)

Put in something like NSAssert(str!=nil, @str is nil); before the call.

—Jens___

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

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


Re: Trouble finding bounding rect of NSAttributedString

2009-11-19 Thread Dave DeLong
Your question has caused me to dig deeper...

It seems that when I handle both the NSViewFrameDidChangeNotification and the 
NSTableViewSelectionDidChangeNotification notifications, the attributedString 
of the textView's layout manager is nil.

This is very interesting...  Here's a bit more of my setup.  I have an 
NSTableView, populated by an NSArrayController.  The textView's data is bound 
to the arrayController's selection.data (which provides an RTF NSData object).  
That works as expected.  I observe the ViewFrameDidChangeNotification on the 
textView, so that as the user types in text and the textview grows, I can also 
resize the superview accordingly.

I was listening to the selectionDidChange notification of the NSTableView, 
because (I've observed) when the data to be shown in the textview is smaller 
than the current size of the textview, it does not resize down to fit the 
content.  This is the behavior that I'm looking for.

However, in both of these methods, the attributedString of the textview is nil, 
thus giving me the strange results I'm seeing.

What would be the appropriate way to get the behavior I'm looking for?

Thanks,

Dave

On Nov 19, 2009, at 4:28 PM, Jens Alfke wrote:

 
 On Nov 19, 2009, at 2:39 PM, Dave DeLong wrote:
 
 When I print out the size or rect that these return, I invariably get either 
 {0, 0} or something absurd like {{1.17076e-318, 2.29357e-314}, 
 {2.30359e-314, 2.1224e-314}} (that's just running the rect through 
 NSStringFromRect())
 
 Make sure the receiver (the NSAttributedString) isn't nil. The Obj-C runtime 
 gives undefined results for a struct-based method return value if the 
 receiver is nil; so you'll end up with garbage like this. (This is because it 
 doesn't know at runtime how large the struct is, so it can't safely fill it 
 in with zeroes.)
 
 Put in something like NSAssert(str!=nil, @str is nil); before the call.
 
 —Jens



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

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