使用自动布局与拓展NSTextViews [英] Using Autolayout with expanding NSTextViews

查看:124
本文介绍了使用自动布局与拓展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.

目前,这是所有在code管理。在 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 文本是之前输入,如果我不为它的高度指定约束自动展开,它defautls 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?

在实施任何指针 intrinsicContentSize 在垂直调整NSTextView?

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];

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

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