How does an automatic NSTextField layout with multiple lines occur when changing the width of an NSTextField?
I have many NSTextFields displaying static text (e.g. labels) in the inspector panel. Since the inspector panel is user editable, I would like the right hand labels to go over multiple lines if necessary.
(The Finder Get Info panel does this.)
But I could not determine the correct combination of auto layout constraints to allow this. In all cases, NSTextFields on the right refuse to wrap. (Unless I explicitly added a height limit that would allow him.)
The view hierarchy is such that each gray bar is a view containing two NSTextFields elements, the name of the property on the left, and the value of the property on the right. As the user resizes the inspector panel, I would like the property value label to automatically change its height as needed.
Current situation:

What I would like:

(Please note that this behavior is different from most of the questions I came up with regarding NSTextFields and auto-layout. These questions wanted the text field to enlarge during user input. In this situation, the text is static, and NSTextField is configured to look like a label.)
Update 1.0
Accepting @hamstergene's suggestion, I subclassed NSTextField and made a small sample application. For the most part, it works now, but now there is a small problem with layouts, which I suspect is the result of the fact that the NSTextField frame is not fully synchronized with what the machine expects. In the screenshot below, the labels on the right side are all vertical with the top constraint. As the window resizes, the Where field gets the correct resizing and wrapping. However, the Kind text box is not reset until I resize the "another pixel" window.
Example. If I resize the window only to the correct width, which the Where text box first wraps around, then I will get the results in the middle image. If I resize the window one more pixel, then the correct position of the Kind field will be set correctly.
I suspect that auto-layout does this and then the frames are explicitly set. I assume that the automatic layout does not see this in this passage, but does it in the next passage and accordingly updates the positions.
Assuming the problem is, how can I report the automatic layout of these changes, I am doing in setFrameSize to start the layout again. (And, most importantly, not to fall into the recursive state layout-setFrameSize-layout-etc ...)
Decision
I came up with a solution that seems to work just the way I hoped. Instead of subclassing NSTextField I just override layout in superview NSTextField . In layout I set preferredMaxLayoutWidth to the text box, and then run the layout. This seems to be enough to make it work mostly, but it leaves an annoying layout problem that is short-term “erroneous”. (See note above).
The solution for this is to call setNeedsDisplay and then all Just Works.
- (void)layout { NSTextField *textField = ...; NSRect oldTextFieldFrame = textField.frame; [textField setPreferredMaxLayoutWidth:NSWidth(self.bounds) - NSMinX(textField.frame) - 12.0]; [super layout]; NSRect newTextFieldFrame = textField.frame; if (oldTextFieldFrame.size.height != newTextFieldFrame.size.height) { [self setNeedsDisplay:YES]; } }