我正在尝试创建一个NSTextFIEld,当用户输入文本时,它会在高度上增长.就像在Mac上的iMessage中那样.以下是视频示例:http://d.pr/v/zWRA6w
我已经设置了这样的NSVIEw,以便我可以在NSTextFIEld周围放置一个自定义设计,并保持默认边框和背景关闭:
这是我的约束.聊天对话在聊天环绕下滚动.
我试图按照this answer创建以下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 }}
但是我的约束和/或代码肯定有问题,因为当我输入框时没有任何反应.
有什么建议?
解决方法 我偶然发现了这个: https://gist.github.com/entotsu/ddc136832a87a0fd2f9a0a6d4cf754ea我不得不更新代码以使用Swift 4:
class autogrowingTextFIEld: NSTextFIEld { var minHeight: CGfloat? = 22 let bottomSpace: CGfloat = 7 // magic number! (the fIEld editor TextVIEw is offset within the NSTextFIEld. It’s easy to get the space above (it’s origin),but it’s difficult to get the default spacing for the bottom,as we may be changing the height var heightlimit: CGfloat? var lastSize: NSSize? 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 var intrinsicContentSize: NSSize { var minSize: NSSize { var size = super.intrinsicContentSize size.height = minHeight ?? 0 return size } // Only update the size if we’re editing the text,or if we’ve not set it yet // 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?) if isEditing || lastSize == nil { //If we’re being edited,get the shared NSTextVIEw fIEld editor,so we can get more info guard let textVIEw = self.window?.fIEldEditor(false,for: self) as? NSTextVIEw,let container = textVIEw.textContainer,let newHeight = container.layoutManager?.usedRect(for: container).height else { return lastSize ?? minSize } var newSize = super.intrinsicContentSize newSize.height = newHeight + bottomSpace if let heightlimit = heightlimit,let lastSize = lastSize,newSize.height > heightlimit { newSize = lastSize } if let minHeight = minHeight,newSize.height < minHeight { newSize.height = minHeight } lastSize = newSize return newSize } else { return lastSize ?? minSize } }}总结
以上是内存溢出为你收集整理的可可 – 使用Swift和自动布局增长更高的NSTextField全部内容,希望文章能够帮你解决可可 – 使用Swift和自动布局增长更高的NSTextField所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)