Multi-line NSTextField not working

前端 未结 2 808
感情败类
感情败类 2021-01-13 03:10

I have been trying to get a multi-line NSTextField to lay out automatically using preferredMaxLayoutWidth. I can’t figure out why this doesn’t work

相关标签:
2条回答
  • 2021-01-13 03:53

    There is an another solution for making the NSTextField multiline with using auto constraints and Interface Builder.

    • Choose Wraps from Layout
    • Choose Character Wrap from Line Break option
    • Uses Single Line Mode must be unchecked

    And add below sample constraints

    • Trailing Space
    • Leading Space
    • Bottom Space
    • Top Space

    with the constants that you want. And don't add any specific height constraint.

    Below some screenshots which might be helpful.

    0 讨论(0)
  • 2021-01-13 04:13

    OK. So it turns out that when an NSTextField is in edit mode, a secret NSTextView (aka the “field editor”) takes over for the editing portion. Text changes are not synced back to the text field until editing has ended. This explains why my sample code only works in non-edit mode.

    You can force the sync to occur by accessing the NSTextField.stringValue property. This is what I’ve done in a NSTextField subclass. I also provided my own implementation of intrinsicContentSize because Apple’s implementation doesn’t work in edit mode and is buggy in non-edit mode.

    0 讨论(0)
提交回复
热议问题