使用 Autolayout 扩展 NSTextViews [英] Using Autolayout with expanding NSTextViews

查看:29
本文介绍了使用 Autolayout 扩展 NSTextViews的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用由一个 NSScrollView 组成,它的文档视图包含许多垂直堆叠的 NSTextViews — 每个都在添加文本时在垂直方向调整大小.

My app consists of an NSScrollView whose document view contains a number of vertically stacked NSTextViews — each of which resizes in the vertical direction as text is added.

目前,这一切都在代码中进行管理.NSTextViews 自动调整大小,但我观察到它们使用 NSViewFrameDidChangeNotification 调整大小,重新计算它们的所有原点以使它们不重叠,并调整它们的超级视图(滚动视图的文档视图) 以便它们都适合并且可以滚动到.

Currently, this is all managed in code. The NSTextViews resize automatically, but I observe their resizing with an NSViewFrameDidChangeNotification, recalc all their origins so that they don't overlap, and resize their superview (the scroll view's document view) so that they all fit and can be scrolled to.

这似乎是自动布局的完美候选!我在第一个文本视图及其容器、最后一个文本视图及其容器以及彼此之间的每个文本视图之间设置了 NSLayoutConstraints.然后,如果任何文本视图增长,它会自动下推"其下方文本视图的来源以满足约束,最终增加文档视图的大小,每个人都开心!

This seems as though it would be the perfect candidate for autolayout! I set NSLayoutConstraints between the first text view and its container, the last text view and its container, and each text view between each other. Then, if any text view grows, it automatically "pushes down" the origins of the text views below it to satisfy contraints, ultimately growing the size of the document view, and everyone's happy!

除了,似乎没有办法让 NSTextView 在基于约束的布局中添加文本时自动增长?使用与之前输入文本时自动扩展的完全相同的 NSTextView,如果我没有为其高度指定约束,它默认为 0 并且不显示.如果我确实指定了一个约束,即使是 >=20 之类的不等式,它也会停留在该大小,并且不会随着添加文本而增长.

Except, it seems there's no way to make an NSTextView automatically grow as text is added in a constraints-based layout? Using the exact same NSTextView that automatically expanded as text was entered before, if I don't specify a constraint for its height, it defautls to 0 and isn't shown. If I do specify a constraint, even an inequality such as >=20, it stays stuck at that size and doesn't grow as text is added.

我怀疑这与 NSTextView 的 -intrinsicContentSize 实现有关,默认返回 (NSViewNoInstrinsicMetric, NSViewNoInstrinsicMetric).

I suspect this has to do with NSTextView's implementation of -intrinsicContentSize, which by default returns (NSViewNoInstrinsicMetric, NSViewNoInstrinsicMetric).

所以我的问题是:如果我将 NSTextView 子类化以根据我的文本布局返回更有意义的 intrinsicContentSize,那么我的自动布局会按预期工作吗?

So my questions: if I subclasses NSTextView to return a more meaningful intrinsicContentSize based on the layout of my text, would my autolayout then work as expected?

关于为垂直调整大小的 NSTextView 实现 intrinsicContentSize 的任何指针?

Any pointers on implementing intrinsicContentSize for a vertically resizing NSTextView?

推荐答案

我在使用 NSTextField 时也遇到了类似的问题,结果发现这是由于视图想要沿垂直方向紧紧地拥抱其文本内容.因此,如果您将内容拥抱优先级设置为低于其他约束的优先级,它可能会起作用.例如:

I had a similar problem with an NSTextField, and it turned out that it was due to the view wanting to hug its text content tightly along the vertical orientation. So if you set the content hugging priority to something lower than the priorities of your other constraints, it may work. E.g.:

[textView setContentHuggingPriority:NSLayoutPriorityFittingSizeCompression-1.0 forOrientation:NSLayoutConstraintOrientationVertical];

在 Swift 中,这将是:

And in Swift, this would be:

setContentHuggingPriority(NSLayoutConstraint.Priority.fittingSizeCompression, for:NSLayoutConstraint.Orientation.vertical)

这篇关于使用 Autolayout 扩展 NSTextViews的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆