Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

the ev

macrumors newbie
Original poster
Jun 14, 2008
9
0
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.

  1. 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?
  2. 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?
  3. 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.
 

the ev

macrumors newbie
Original poster
Jun 14, 2008
9
0
Solved point number 1

Solved my first question. Apparently assigning a value to a NSTextField via setStringValue doesn't trigger a textDidChange notification, which is why it wasn't updating. I added a [CCDGrowingTextField sizeToFit] call outside of the class where I update the contents of the field and it works just fine.

Which just leaves me with two questions! Anyone?
 

the ev

macrumors newbie
Original poster
Jun 14, 2008
9
0
Solved (well, not really; "worked-around" more-like) my second question. If I set the text in the field to be center justified, I think the jitter becomes part of the text's expanding movement, so it's not noticeable.

Fortunately, I want it to be center-justified. However, if I didn't, I still have no idea how I'd fix that.

Working on my third question now... please chime in if you know anything! It would be a great help!
 

pjrobertson

macrumors 6502a
Nov 14, 2007
533
4
I'm probably newer to cocoa than you :p
but....
can't you do the resize business from within IB? Click the NSTextField and then go tools -> inspector and go to the size tab. You can click the red lines to made them bold so that the window resizes with the app.
 

the ev

macrumors newbie
Original poster
Jun 14, 2008
9
0
I'm probably newer to cocoa than you :p
but....
can't you do the resize business from within IB? Click the NSTextField and then go tools -> inspector and go to the size tab. You can click the red lines to made them bold so that the window resizes with the app.

I want the window to resize when the text field grows, not for the text field to grow when the window is resized (I'm not planning on giving my users an option to resize this window). I believe it's a one-way street. :(
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.