NSTextField intrinsicContentSize width is always -1 (NSViewNoInstrinsicMetric)

I've got a pair of editable text fields in a view using autolayout. (For completeness, these are inside an

NSTableRowView
in an editable table, but I don't think that should matter.)


I'd like the fields to resize their width based on their content, especially once they are edited.


However,

NSTextField
is not cooperating. I added some code to log the
intrinsicContentSize
whenever the edit field ends editing, and the value is always
{ NSViewNoInstrinsicMetric, 15 }
, meaning the field has no intrinsic width.


I've seen a number of posts about multi-line

NSTextField
and vertical (height) resizing, wrapping, and so on, but I can't find any questions or answers that apply here. The fields are both set to "use single line", "clip", and "scroll".


I've tried sending the fields

updateConstraints
and
invalidateIntrinsicContentSize
messages, but they don't seem to have any effect.


I know I can create a subclass of NSTextField and hack this, but I don't understand why this isn't working in the first place. Or maybe

NSTextField
simply doesn't have an intrinsic width, but I can't find that documented anywhere.

Replies

It’s correct that editable fields don’t have an intrinsic width. Typically they clip their contents to an arbitrary width, and during editing the field editor allows you to scroll through the text if it’s too large. In this case it sounds like you’ll want to constrain the field to its table cell so that it derives its width from the table column’s width.