使用Swift和自动布局增加更高级别的NSTextField

Cli*_*rum 6 cocoa nstextfield autolayout

Xcode 9.1,Swift 4

我正在尝试创建一个NSTextField随着用户输入文本而增长的高度.就像在Mac上的iMessage中那样.这是一个视频示例:http://d.pr/v/zWRA6w

我已经设置了NSViews这样的设置,以便我可以在周围设置自定义设计,NSTextField并保持默认边框和背景关闭:

在此输入图像描述

这是我的约束.聊天对话在聊天环绕下滚动. 在此输入图像描述 在此输入图像描述 在此输入图像描述

我试着按照这个答案创建了以下Swift版本:

class ResizingTextField: NSTextField{
  var isEditing = false
  override func textDidBeginEditing(_ notification: Notification) {
    super.textDidBeginEditing(notification)
    isEditing = true
  }
  override func textDidEndEditing(_ notification: Notification) {
    super.textDidEndEditing(notification)
    isEditing = false
  }
  override func textDidChange(_ notification: Notification) {
    super.textDidChange(notification)
    self.invalidateIntrinsicContentSize()
  }
  override public var intrinsicContentSize: CGSize {
    if isEditing{
      let fieldEditor = self.window?.fieldEditor(false, for: self)
      if fieldEditor != nil{
        if let cellCopy = self.cell?.copy() as? NSTextFieldCell{
          cellCopy.stringValue = fieldEditor!.string
          return cellCopy.cellSize
        }
      }
    }
    return self.cell!.cellSize
  }
}
Run Code Online (Sandbox Code Playgroud)

但是我的约束和/或代码肯定有问题,因为当我输入框时没有任何反应.

有什么建议?

Cli*_*rum 3

我遇到了这个有效的方法:https://gist.github.com/entotsu/ddc136832a87a0fd2f9a0a6d4cf754ea

\n\n

我必须稍微更新一下代码才能使用Swift 4

\n\n
class AutoGrowingTextField: NSTextField {\n\n  var minHeight: CGFloat? = 22\n  let bottomSpace: CGFloat = 7\n  // magic number! (the field editor TextView is offset within the NSTextField. It\xe2\x80\x99s easy to get the space above (it\xe2\x80\x99s origin), but it\xe2\x80\x99s difficult to get the default spacing for the bottom, as we may be changing the height\n\n  var heightLimit: CGFloat?\n  var lastSize: NSSize?\n  var isEditing = false\n\n  override func textDidBeginEditing(_ notification: Notification) {\n    super.textDidBeginEditing(notification)\n    isEditing = true\n  }\n  override func textDidEndEditing(_ notification: Notification) {\n    super.textDidEndEditing(notification)\n    isEditing = false\n  }\n  override func textDidChange(_ notification: Notification) {\n    super.textDidChange(notification)\n    self.invalidateIntrinsicContentSize()\n  }\n\n  override var intrinsicContentSize: NSSize {\n    var minSize: NSSize {\n      var size = super.intrinsicContentSize\n      size.height = minHeight ?? 0\n      return size\n    }\n    // Only update the size if we\xe2\x80\x99re editing the text, or if we\xe2\x80\x99ve not set it yet\n    // If we try and update it while another text field is selected, it may shrink back down to only the size of one line (for some reason?)\n    if isEditing || lastSize == nil {\n\n      //If we\xe2\x80\x99re being edited, get the shared NSTextView field editor, so we can get more info\n      guard let textView = self.window?.fieldEditor(false, for: self) as? NSTextView, let container = textView.textContainer, let newHeight = container.layoutManager?.usedRect(for: container).height\n      else {\n          return lastSize ?? minSize\n      }\n      var newSize = super.intrinsicContentSize\n      newSize.height = newHeight + bottomSpace\n\n      if let heightLimit = heightLimit, let lastSize = lastSize, newSize.height > heightLimit {\n        newSize = lastSize\n      }\n\n      if let minHeight = minHeight, newSize.height < minHeight {\n        newSize.height = minHeight\n      }\n\n      lastSize = newSize\n      return newSize\n    }\n    else {\n      return lastSize ?? minSize\n    }\n  }\n}\n
Run Code Online (Sandbox Code Playgroud)\n