Xcode 9.1, Swift 4
I'm trying to create an NSTextField
that grows in height as the user enters text. Just like the one in iMessage on a Mac. Here's an example video: http://d.pr/v/zWRA6w
I have set up the NSViews
like this so that I can put a custom design around the NSTextField
and just leave its default border and background off:
Here are my constraints. The chat conversation scrolls underneath the chat wrap.
I tried to follow this answer and created the following Swift version:
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
}
}
But there must be something wrong with my constraints and/or code, as nothing happens when I type in the box.
Any suggestions?