CocoaDev

Edit AllPages

This textfield expands and contracts as it is edited and resized, and behaves in a similar manner to the input field in Apple’s iChat message windows.

Superviews of the textfield and the window containing the textfield autosize accordingly.

– AndrewBowman

IFVerticallyExpandingTextField.h

/* Copyright (c) 2006, Andrew Bowman. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

/* IFVerticallyExpandingTextField

This textfield expands and contracts as it is edited and resized, and behaves in a similar manner to the input field in Apple’s iChat message windows.

Superviews of the textfield and the window containing the textfield autosize accordingly.

*/

#import <Cocoa/Cocoa.h>

enum { IFVerticalPadding = 5 };

@interface IFVerticallyExpandingTextField : NSTextField { BOOL superviewsExpandOnGrowth; BOOL isCollapsed; NSMutableArray *viewMaskPairs; }

@end

IFVerticallyExpandingTextField.m

/* Copyright (c) 2006, Andrew Bowman. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

#import “IFVerticallyExpandingTextField.h”

// Private helper class for associating NSViews with autoresizingMasks @interface IFViewMaskPair : NSObject {
NSView *view; unsigned int savedAutoresizingMask; }

@end

@implementation IFViewMaskPair

@end

@interface IFVerticallyExpandingTextField (PRIVATE)

@end

@implementation IFVerticallyExpandingTextField

/* Private methods */

}

/* Overridden methods */

@end


Give it a try! You should be able to throw this into a project, read the files in Interface Builder, and use the custom class pane to make an NSTextField into an IFVerticallyExpandingTextField. You’ll need to set the layout and linebreaking attributes for word wrapping for this to work.

Although expansion should work properly when the textfield is embedded in a subview, I’m having some trouble dealing with NSScrollViews. The field expands into the scrollview’s content view, but none of the controls on the scrollbar appear. Some help here would be appreciated.

Added the setStringValue method, can’t believe I forgot that. In my test application I thought I saw a noticeable difference in speed when calling setString on the field editor rather than setStringValue on super, but maybe it was just my imagination. Can anyone confirm this?

Also added a slight time delay in this method to prevent display problems when autosizing.

Added the setHidden method. I haven’t tested this one out, so I don’t know what the consequences are if the field is set visible when a view has been moved up to where the field should be. —- You’re not using setHidden: correctly. setHidden: visually hides the field, but – by design – does not hide it for layout purposes. You want to add a new method, setCollapsed: or setMinimized:. You shouldn’t need to override setHidden. —- Thanks for the correction. I’ve changed the code to use setCollapsed. Have you had the chance to play around with this? Any feedback’s welcome. – AndrewBowman —- I know this post is a bit old, but it was a good starting point for me. Here’s a suggestion for improving the code: use a separate NSTextView as a utility object to determine the required height rather than the field editor. This avoids the issue with moving first responder around unnecessarily. You’d also probably want to cache the text view rather than create it each time. Here’s a sample of what I’m talking about:

NSTextView *utilityTextView = [[NSTextView alloc] initWithFrame:[self frame]];
[utilityTextView setString:[self stringValue]];
utilityTextView layoutManager] glyphRangeForTextContainer:[utilityTextView textContainer; // force layout
CGFloat newHeight = NSHeight(utilityTextView layoutManager] usedRectForTextContainer:[utilityTextView textContainer);

– Sean Rich