Based on How to let NSTextField grow with the text in auto layout?
Also don't set size contraints, just let it be.
The method intrinsicContentSize
in NSView
returns what the view itself thinks of as its intrinsic content size.
NSTextField
calculates this without considering the wraps
property of its cell, so it will report the dimensions of the text if laid out in on a single line.
Hence, a custom subclass of NSTokenField
can override this method to return a better value, such as the one provided by the cell's cellSizeForBounds:
method:
-(NSSize)intrinsicContentSize
{
if ( ![self.cell wraps] ) {
return [super intrinsicContentSize];
}
NSRect frame = [self frame];
CGFloat width = frame.size.width;
// Make the frame very high, while keeping the width
frame.size.height = CGFLOAT_MAX;
// Calculate new height within the frame
// with practically infinite height.
CGFloat height = [self.cell cellSizeForBounds: frame].height;
return NSMakeSize(width, height);
}