Okay, I found this neat code snippet that allows me to make a NSTextField that resizes as text is entered. While I'm still pretty new at all of this, I managed to edit it and tweak it the way I wanted it to work, but I still have three problems that I can't seem to figure out.
To illustrate all three of these problems, here's a link to a short video screencast I've made of how my App works now. (NOTE: the text jitters more or less depending on what letter I use; here's the 'o' really only jitters once, but if I use another letter it jitters much more. The jitter also only happens while text is being entered.)
Here's the modified CCDGrowingTextField code that I'm using:
I appreciate any help/insight anyone can give me! Thanks in advance!
Edit: The video's dimensions are the same as the Application Window, which is non-resizable by the user.
- I've two CCDGrowingTextField's in my App; one that is editable, and the other than basically mirrors the text in the first but is not editable. The resize works great on the one that you type into directly, but the second one doesn't seem to resize at all. Any ideas?
- I would like the NSTextField type on the first (editable) field to pill-shaped (specified in Interface Builder), but when I'm typing in text, the text jitters from side to side very slightly. Is there anyway I can eliminate that jitter?
- My application window starts very small. I would like to have the window resize horizontally depending on the width of the CCDGrowingTextFields, so if the fields grow the window edge will grow with them. How can I accomplish this? Also, along the same lines, once my TextField approaches the edge of the window, the background changes slightly. Can I get rid of this glitch too?
To illustrate all three of these problems, here's a link to a short video screencast I've made of how my App works now. (NOTE: the text jitters more or less depending on what letter I use; here's the 'o' really only jitters once, but if I use another letter it jitters much more. The jitter also only happens while text is being entered.)
Here's the modified CCDGrowingTextField code that I'm using:
Code:
#import <Cocoa/Cocoa.h>
@interface CCDGrowingTextField : NSTextField
{
float defaultLeftMargin;
float defaultRightMargin;
NSRect defaultFrame;
}
@end
Code:
#import "CCDGrowingTextField.h"
@interface CCDGrowingTextField(Private)
- (void)updateDefaultMargins;
- (void)resetFrameToDefault;
- (void)sizeToFit;
- (void)textDidChange: (NSNotification *)notification;
@end
@implementation CCDGrowingTextField
- (id)initWithFrame: (NSRect)frame
{
if ((self = [super initWithFrame:frame])) {
defaultFrame = frame;
}
return self;
}
- (id)initWithCoder: (NSCoder *)decoder
{
if ((self = [super initWithCoder:decoder])) {
defaultFrame = [self frame];
}
return self;
}
- (void)awakeFromNib
{
[self updateDefaultMargins];
}
@end // CCDGrowingTextField
@implementation CCDGrowingTextField(Private)
- (void)updateDefaultMargins
{
NSRect myFrame = [self frame];
NSRect superBounds = [[self superview] bounds];
defaultLeftMargin = NSMinX(myFrame);
defaultRightMargin = superBounds.size.width - NSMaxX(myFrame);
}
- (void)resetFrameToDefault
{
NSRect myFrame = [self frame];
myFrame.size.width = defaultFrame.size.width;
[self setFrame:myFrame];
}
- (void)sizeToFit
{
[super sizeToFit];
NSRect myFrame = [self frame];
float curRightMargin = [[self superview] bounds].size.width - NSMaxX(myFrame);
if (curRightMargin > defaultRightMargin) {
[self resetFrameToDefault];
}
}
- (void)textDidChange: (NSNotification *)notification
{
[super textDidChange:notification];
[self sizeToFit];
}
@end // CCDGrowingTextField(Private)
I appreciate any help/insight anyone can give me! Thanks in advance!
Edit: The video's dimensions are the same as the Application Window, which is non-resizable by the user.