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

mackonsti

macrumors newbie
Original poster
Jan 14, 2009
12
9
Muppet Show
Greetings to the community, I am an inexperienced user trying to learn via the simple projects creation process... I request your assistance as I have not found a working solution for two whole days now, digging the Xcode Help and Google.

I have a simple MacOS X (10.6.x) application that creates an extra NSMenuItem on the status bar. On my AppDelegate, I create an object that loads the NSView (from MainMenu.xib):

Code:
	// Add menu entry for NSView object
	NSMenuItem* newItem = [[NSMenuItem alloc] initWithTitle:@"Placeholder" action:nil keyEquivalent:@""];
	[newItem setView:[B]theView[/B]];
	[newItem setEnabled:NO];
	[newItem setTarget:self];
	[[B]calendarMenu[/B] insertItem: newItem atIndex:0];
	[newItem release];

In my MainMenu.xib, besides the menu structure, I have another object set as NSView and under it, NSTextField (occupying all the space of the view).

Later on, I set some string to NSTextField but I cannot adjust the height accordingly; it's either too much or crops:

Code:
	// Crop last newline character
	NSString *menuCelebrations = [NSString stringWithString:[[B]celebName[/B] substringToIndex:([[B]celebName[/B] length] - 1)]];
	NSFont *msgFont = [NSFont menuBarFontOfSize:12.0];

	// Set attributes
	[[B]theTextField[/B] setFont:msgFont];
	[[B]theTextField[/B] setStringValue:[B]menuCelebrations[/B]];

The top menu now displays the longer text that I want, wrapped etc. but I cannot adjust the height properly with something like:

Code:
	[[B]theView[/B] setFrame:
	 NSMakeRect([theView frame].origin.x,
				[theView frame].origin.y,
				[B]calendarMenu.size.width[/B],
				[B]newHeightFloat[/B])];

Tried things like these for automatic resize, but to no avail:
Code:
	[[B]theTextField[/B] sizeToFit];
	[[B[B]]theTextField[/B] autoresizesSubviews];
	[[B]theView[/B][/B] setAutoresizingMask:NSViewHeightSizable];
	[theView setNeedsDisplay:YES];

Needless say that I tried numerous combinations in Interface Builder (where the text field resizes with the view etc.). Nothing worked... I still cannot figure out a way to calculate and set the height to either NSView or NSTextFieled that is the sub-object.

I found ideas and code over www.stackoverflow.com and www.cocoabuilder.com but people mention the use of NSLayoutManager, NSTextStorage and NSTextContainer, that Apple suggests somewhere in the documentation (look for chapter/reference "Calculating Text Height" that has some code, but fails to be implemented); others suggest that Apple's calculates the box but rendering that box does miss some pixels at the bottom.

A reference to Matt Neuburg's work (instead of NSLayout that Apple suggests) that makes proper calculation (with a sample project) seems to work well but on NSTextView objects...

Must I create a separate/custom class for this NSView? Should I prefer NSTextView to NSTextField?

Did anyone manage to adjust the height (only) to the proper size? I would surely appreciate some help with a code sample...

... Am I digging to deep without proper understanding of the methods in place?

Thank you in advance.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.