Re: Text scrolling with Finder-like label editing. Will a raw field editor (NSTextView) do this, or should I use an NSTextField or NSTextFieldCell? [SOLVED]
Re: Text scrolling with Finder-like label editing. Will a raw field editor (NSTextView) do this, or should I use an NSTextField or NSTextFieldCell? [SOLVED]
- Subject: Re: Text scrolling with Finder-like label editing. Will a raw field editor (NSTextView) do this, or should I use an NSTextField or NSTextFieldCell? [SOLVED]
- From: Luke Evans <email@hidden>
- Date: Wed, 4 Mar 2009 14:23:11 -0800
OK. I managed to get things working.
The main feature of the change was simply to create an NSScrollView
when editing starts and drop the acquired and configured NSTextView
inside it. This was actually very straight forward - and finding that
bit of documentation provided the confidence that wrapping a scroll
view in this way was the Right Thing.
The thing that has been fiddly and sensitive to getting upset (causing
bad sizing) is the use of -sizeToFit on the NSTextView.
What I've found is this:
I have to set the text container initially to the maximum width of the
label area (i.e. as wide as the label is allowed to grow), and with
the maximum height (FLT_MAX).
In the method that resizes both the text view and its enclosing
NSScrollView, I need to do two lots of sizing. Here's what I have at
the moment:
- (void)adjustEditorForContents {
// XXX? The two-stage sizing by 'sizeToFit' is a bit of magique
noir. How does one do this in one go?
// First get the active field editor to produce its minimum size and
remember this for the proposed editor rect.
[activeFieldEditor setFrameSize:NSMakeSize(MIN_TEXT_CONTAINER_WIDTH,
[self twoLineHeight]/2)];
[activeFieldEditor sizeToFit];
NSSize editorProposedSize = [activeFieldEditor frame].size;
// Now get the field editor to size naturally, allowing for multi-
line sizes (and scrolling)
[activeFieldEditor setFrameSize:NSMakeSize(MIN_TEXT_CONTAINER_WIDTH,
FLT_MAX)];
[activeFieldEditor sizeToFit];
// Determine where this should go (centred at the top of the text area)
NSRect editorFrame = ESRectConstructWithinAndClip([self
labelAreaRect], ES_RECT_CENTRE | ES_RECT_TOP, editorProposedSize);
[fieldEditorScroller setFrame:editorFrame];
// Redraw the underlying view to get the focus highlight in the right
place
[self setNeedsDisplay:YES];
}
I'm not happy with this code - but mostly because it was arrived at
empirically, I don't fully understand it.
To get the actual size for the editor bounds, you seem to need to
constrain smaller than the text might actually need to fit.
However, in order to have the correct scrolling behaviour, you have to
do this sizing again, this time allowing effectively having the height
of the text view unconstrained when sizing is done.
As I've been writing this, I've done another search and found this:
http://www.omnigroup.com/mailman/archive/macosx-dev/1999-April/007703.html
... which seems to delve into the arcane behaviour of -sizeToFit
(thanks Mike Ferris!).
Anyway, I've got a nice dynamically resizing label editor now, so I'm
happy :-)
-- lwe
On 4-Mar-09, at 12:08 PM, Eric Gorr wrote:
If you succeed, I would be interested.
_______________________________________________
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