Is there any way to automatically resize the height of a NSTokenField (keeping the width constant) using constraints?
-sizeToFit
should work, but it doesn't. If I set a constraint to keep the width constant and call this method it ignores the constraints and resizes the width only (when what I want is to resize the height only).
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
inNSView
returns what the view itself thinks of as its intrinsic content size.NSTextField
calculates this without considering thewraps
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'scellSizeForBounds:
method:The
cellSizeForBounds
method of the token field does return the correct size, so you can implement it like this (custom subclass, in Swift):