I have a view that contains two NSTextFieldCell
s. The size at which these cells are drawn is derived from the size of the view, and I want the text in each cell to be the largest that will fit in the derived size of the cell. Here's what I have, which doesn't set the font size:
- (void)drawRect:(NSRect)dirtyRect {
/*
* Observant readers will notice that I update the whole view here. If
* there is a perceived performance problem, then I'll switch to just
* updating the dirty rect.
*/
NSRect boundsRect = self.bounds;
const CGFloat monthHeight = 0.25 * boundsRect.size.height;
NSRect monthRect = NSMakeRect(boundsRect.origin.x,
boundsRect.origin.y + boundsRect.size.height
- monthHeight,
boundsRect.size.width,
monthHeight);
[monthCell drawWithFrame: monthRect inView: self];
NSRect dayRect = NSMakeRect(boundsRect.origin.x,
boundsRect.origin.y,
boundsRect.size.width,
boundsRect.size.height - monthHeight);
[dayCell drawWithFrame: dayRect inView: self];
[[NSColor blackColor] set];
[NSBezierPath strokeRect: boundsRect];
}
So I know that I can ask a string what size it would take for given attributes, and I know that I can ask a control to change its size to fit its content. Neither of those seems applicable: I want the content (in this case, the cell's stringValue
) to size to fit the known rect dimensions, with the attributes needed to achieve that being unknown. How can I find the needed size? Assume that I know what font I'll be using (because I do).
update Note: I don't want to truncate the string, I want to grow or shrink it so that the whole thing fits, with the largest text size possible, into the provided rect.