简体   繁体   中英

Multi-line NSTextField not working

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.

class ViewController: NSViewController {
   override func viewDidLoad() {
      super.viewDidLoad()

      let textField = NSTextField()
      textField.cell!.usesSingleLineMode = false
      textField.cell!.wraps = true
      textField.cell!.lineBreakMode = .byCharWrapping
      view.addSubview(textField)

      textField.translatesAutoresizingMaskIntoConstraints = false
      NSLayoutConstraint.activate([
         textField.topAnchor.constraintEqual(to: view.topAnchor),
         textField.leadingAnchor.constraintEqual(to: view.leadingAnchor),
         textField.widthAnchor.constraintEqual(toConstant: 20)
      ])
      textField.preferredMaxLayoutWidth = 20
      textField.stringValue = "abcdefghijklmnopqrstuvwxyz"

      view.needsLayout = true
      view.layoutSubtreeIfNeeded()
      print("Intrinsic content size: \(textField.intrinsicContentSize)")
      print("Fitting size: \(textField.fittingSize)")
   }
}

This prints:

Intrinsic content size: (-1.0, 21.0)

Fitting size: (20.0, 21.0)

(21.0 is the size for a single line.)

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.

约束

NSTextField设置

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.

The most important that you have to take in account is Horizontal Compression Resistance Priority , understanding compressions of the siblings you need to decrease your NSTextField compression value. For example, you can find out starting from the low number (250).

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM