越来越多的NSTextView以适合内容正在剪切文本的最后一行 [英] Growing NSTextView to fit contents is clipping last line of text

查看:107
本文介绍了越来越多的NSTextView以适合内容正在剪切文本的最后一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个NSTextView,它会随着用户的输入而垂直增长,并在高度达到最大值后滚动。这类似于Messages作品中的文本视图。

I'm trying to create an NSTextView that grows vertically as the user types and scrolls once the height has reached a maximum. This is similar to the text view in Messages works.

我的第一次尝试是使用委托来侦听文本更改并调整与NSTextView的滚动视图关联的高度约束:

My first attempt uses the delegate to listen for text changes and adjust the height constraint associated with the NSTextView's scroll view:

- (void)textDidChange:(NSNotification *)notification
{
    NSTextView *textView = self.textView;
    NSRect usedRect = [textView.textContainer.layoutManager usedRectForTextContainer:textView.textContainer];
    NSLog(@"DEBUG: used rect: %@", NSStringFromRect(usedRect));
    self.textViewHeightConstraint.constant = MIN(80.f, MAX(usedRect.size.height, 30.f));
}

这几乎可行:文本视图(滚动视图)的高度随着类型,但是,文本的最后一行被剪切:

This almost works: the text view's (scroll view's) height is updated as I type, however, the last line of text is clipped:

一旦滚动视图达到其最大高度并开始滚动,效果很好。我尝试强制在封闭的滚动视图上进行显示/布局/约束更新,但是没有运气。我的猜测是滚动视图的剪辑视图未正确更新,并且剪辑了文本视图的底部。

Once the scroll view reaches it's max height and begins scrolling it works nicely. I've tried forcing a display/layout/constraint update on the enclosing scroll view with no luck. My guess is the clip view of the scroll view isn't updating correctly, and it's clipping the bottom of the text view. Is there any way to force the clip view/scroll view to update appropriately when the constraint changes?

推荐答案

我相信您是在约束发生变化时强制剪辑视图/滚动视图正确更新的任何方法。将NSTextView的高度设置为textContainer所需的大小,但您不考虑文本视图正在添加的插图。尝试将-[NSTextView textContainerInset] 的高度添加到计算出的高度中。

I believe that you are setting the NSTextView's height to the size needed for the textContainer but you aren't accounting for the inset that the text view is adding. Try adding the height of -[NSTextView textContainerInset] to your calculated height.

文档并未真正指定,但我假设 textContainerInset 返回宽度/的总插入量高度,而-[NSTextView textContainerOrigin] 会将其大致分成两半。

The documentation doesn't really specify, but I'm assuming that textContainerInset returns the total inset for width/height on all sides and that -[NSTextView textContainerOrigin] would roughly split that in half.

这篇关于越来越多的NSTextView以适合内容正在剪切文本的最后一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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