layoutBox/PinLayout

resizing of NSTextField

tcurdt opened this issue · 5 comments

I have a multiline NSTextField where I set an attributed string with a layout defined as such:

        text.pin.center()
            .width(80%)
            .maxWidth(600)

While the width seems to behave correctly, the height does not adjust to the content. With the width becoming smaller the text wraps and the lower lines are just getting cut off (instead of growing the height).

How can I make the height adjust to text size? I am calling

            text.sizeToFit()
            needsLayout = true

on the assignment of the text - but that's just once.

You must call sizeToFit(.width) so the text field will adjust the its to match its new width.

text.pin.center().width(80%).maxWidth(600).sizeToFit(.width)

See https://github.com/layoutBox/PinLayout#adjusting-size

Aaaaaah. That works!

It seems like I only set the needsLayout = true on text assignment and then this will call the appropriate sizeToFit during the layout process. Correct, @lucdion?

When the text change, you must relayout your text field, i.e. call the text.pin.center().width(80%).maxWidth(600).sizeToFit(.width), you don't need to do needsLayout = true.

I have my layout code in override func layout() which I guess is why needsLayout = true seems to work OK for me, too.

I wasn't aware I can just call a portion of the layout and then the rest will still fall into place!?

Yes, you're right about needsLayout = true, it will call layout().
PinLayout always start from the current view's frame and apply any new commands.